Variables in Java

Variable is nothing but the container which is use to hold or store data. In any programming language data is stored in memory and memory is divided into cells. Since the value stored in each cell may change, so names given to these cells are called variable .Variable names are constructed by using alphabets, numbers and using special symbols i.e dollar and underscore. Variables hold only one value at time. Example of variable shown below.

Since the location whose name is a can hold different values at different times a is known as a variable.Example of variable is shown below.

Variable declaration:
In Java, all variables must be declared before they can be used. you can declare multiple variables simultaneously by just separating all variables by comma(,) .  Syntax of a variable declaration is shown below:

syntax: [data type] [variable name];

Variable Initialization:
There are different ways to assign value to variables but here we will see only direct assignment means assigning value to variables at the time of variable declaration. Variable value assignment at the time variable declaration is shown in below example1 and example2 shows declaration multiple variables simultaneously and value assignment.

Example of variable:

You may also like...

Leave a Reply