Understanding Variables in Java

Following software versions which are used:

  • Eclipse: Oxygen
  • Java: 1.8

The variables in Java can be declared to store data in the program. There are few variations how we can declare a variable in a programming language. The following program will demonstrate how we can declare and then initialize an int variable.

Output: 1

Multiple variable initializations within a statement

When it comes to multiple variable initializations, Java also supports multiple initializations within a single statement within a statement.  We can also merge the declaration and initialization of multiple variables within a single. To initialize multiple variables, we need to use a comma-separated list.

Output: 2

Initialization of variable through arithmetic operation

After the arithmetic operation, the final result would be assigned to the variable.  Assignment of the result is done using the assignment operator (=) and assignment sequence is from right to left. If you make use of bracket within the operation, operation inside the bracket would be performed first.

Output: 3

Assigning one primitive variable to another

We can also assign a value to a variable by using another variable. This is also called as copying the value of one variable into another.


  • Assign any integer value to an int
  • Assign the variable value to a variable.

It is well explained in the below example.

Output: 1

Note: Assignment operation can be done in single line also.

Naming Convention

  • The names of variables are case sensitive “value” and “Value” both are different.
  • Space is not permitted in variable names.

Contributed by: Poonam Tomar


Full stack Java Developer

   Buy me a coffee!



Download file

Leave a Reply

1 Comment threads
2 Thread replies
Most reacted comment
Hottest comment thread
2 Comment authors
adminChris Myll Recent comment authors
newest oldest most voted
Notify of
Chris Myll

public static void main(String[] args) {
int value1 = 1;
int value2 = value1;
System.out.println (value2);

Output: 3

Maybe this is a typo?


Yes, it is. The article will be updated soon.


Issue fixed!

Close Menu