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:-
1 |
<type> <variable_name>; |
or
1 |
<type> <name> = <expression>; |
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.)
1 |
int counter; |
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:-
1 |
<type> <var1,var2...varN>; |
Example:
1 |
int i,j,k; |
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:-
1 2 3 4 5 6 7 8 |
public class Main { public static void main(String[] args) { int ctr; ctr = 5; // Assignment Statement System.out.println(ctr); } } |
Output:-
1 |
5 |
Initializing Variable In Java
In Java, it is possible to declare and assign some initial value to a variable in a single statement.
Syntax:-
1 |
<type> <name> = <expression>; |
Example:-
1 |
int counter=3; |
Multiple assignments In Java
In Java, it is possible to assign multiple variables the same value in a single statement as follows.
Example:-
1 2 3 4 5 6 7 8 9 10 |
public class Main { public static void main(String[] args) { int ctr, i, j; ctr= i= j= 5; System.out.println(ctr); System.out.println(i); System.out.println(j); } } |
Output:-
1 2 3 |
5 5 5 |