Pages

Thursday, September 8, 2016

what is variable (java)

Explanation

Variable : Variable means ( various ) . Variables are use not only in java but are use in every programming languages.Variable take data and store it in the memory.

Why use variables

Let me explain you :: using an example
If you have a big program and you are need to use a number 10 many many times, So you will type this number many many times. With that typing you will indeed get tired.And sometimes if you have to type a big number from the keyboard, You can do a mistake as well.
But if you will give that value a name. That way you can type the name from the keyboard anywhere without any error in short time.So variable works the same thing if you will just give a name to a value .So to that value, You can use every where you just have to type the name.Ie

Variable syntax in java

variable declaration in java

datatype name ;

When you declare a variable its reserve a memory with a specific size
Note : Size depends on data type
in the computer memory.
To declare a variable follow these steps

1.In the declaration of variable you have to put first the datatype befor giving the name to a variable what is data type
Click here to learn data type
2. After writing data type you should then put the name for your variable

Note : The name could be any name depends on users. What they like to give.

What can include a variable name. A variable name can includes all these (a,b,c....z) , (A,B,C...Z) ,

A variable name can iclude(0,1,2,3...9) ( Note : A variable name can't include a number in the start,But you can put it in the middle or end))
A variable can't include any space.
A variable can include ( _ ) symbol for space.

3. After giving name you can initiallize your variable.But if you like to initiallize your variable in another statement So put simi colom ( ; )at the end.

Remember : Always put (; ) simi colom at the end of every statements

Write a program to declare a variable


 public static void main(String []args){

// declaration of an int variable
     int  abc

   
 }

Initiallization of a variable


 public static void main(String []args){

// declaration of an int variable
     int  abc;
// variable initiallization
     abc = 20;
 

    }

In initiallization we assign a value to a variable. We put a value in a variable So java compiler than store this assigned value to the computer memory .And when ever if you will call this variable it will automatically call the value you assigned.

In above example we assigned 20 to our variable abc , Which is of type integer ( an integer reserve 4 byte space in memory for more info Click me ). And when ever if you will call abc variable so 20 will print out.

Write a program to print out a variable value


public static void main(String []args){

 // declaration of an int variable
     int  abc;
 // variable initiallization
      abc = 20;
// print out variable abc
     System.out.print(abc);
        

           }

Variable call

To call a variable you need access to the variable name. In the above example (abc ) is the name of the variable which stored the value (20). So when you call it to the out put console , It will show 20 for you

Out put:

20

No comments:

Post a Comment