Java Variable

In this tutorial you will learn about the Java Variable and its application with practical example.

Java Variable

A Variable is an identifier used to refer to a memory location in computer memory that holds a value for that variable, this value can be changed during the execution of the program. When you create a variable in java, this means you are allocating some space in the memory for that variable. The size of the memory block allocated and the type of the value it holds are completely dependent upon the type of variable.

Rules for naming a variable in Java

Constant and variable names cannot contain whitespace characters, mathematical symbols, arrows, private-use (or invalid) Unicode code points, or line- and box-drawing characters. Naming a variable in Java Programming is an important task and must follow some rules, listed below –

  • The variable name can consist of letters and alphabets.
  • Keywords are not allowed to use as a variable name.
  • Blank spaces are not allowed in variable names.
  • The first character of the variable should always be an alphabet and cannot be a digit.
  • Variable names are case sensitive i.e. UPPER and lower case are significant.
  • Special characters like #, $ are not allowed except the underscore.
  • Mathematical symbols, arrows, and Unicode code points are not allowed.

Recommendation:- Variable name must be readable and should be relative to its purpose.

Declaring Variables In Java

In Java, variables must be declared before they are used. Variables are declared using the data type followed by the variable name that you want to declare.

Syntax:-

or

Example:-

Here is an example of declaring an integer, which we’ve called counter. (Note the semicolon at the end of the line; that is how your compiler separates one program statement from another.)

This statement means we’re declaring some space for a variable called counter, which will be used to store integer data. Note that we must specify the type of data that a variable will store.

Declaring multiple variables

In Java, it is possible to declare multiple variables of the same type in a single statement separated by commas, with a single type annotation as follows-

Syntax:-

Example:

Variable assignment In Java

The assignment operator (=) is used to assign values to a variable, the operand on the left side of the assignment operator (=) indicates the name of the variable and the operand on the right side of the assignment operator (=) indicates the value to be stored in that variable.

Example:-

Output:-

Initializing Variable In Java

In Java, it is possible to declare and assign some initial value to a variable in a single statement.

Syntax:-

Example:-

Multiple assignments In Java

In Java, it is possible to assign multiple variables the same value in a single statement as follows.

Example:-

Output:-

In this tutorial we have learn about the Java Variable and its application with practical example. I hope you will like this tutorial.