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.

Demonstration

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

It is well explained in the below example.

Output: 3

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

 


VIEW COURSES

Buy me a coffee!

 

PayTM QR (INDIA)

  • Get video tutorials
  • Project files set
  • Get Certified

Leave a Reply

avatar
  Subscribe  
Notify of
Close Menu