|What is Java?|
|Java vs Other Languages|
|Java on Internet|
|Java First Program|
|Java Commandline argument|
|Java Types of Variables|
|Java Getting Input from User|
|Java Math Functions|
|Java if statement|
|Java if-else statement|
|Java nested if-else|
|Java else-if ladder|
|Java Switch case statement|
|Java Nested Switch Case|
|Java Ternary/Conditional operator|
|Java while loop|
|Java do-while loop|
|Java for loop|
|Java Enhanced loop/for each loop|
|Java Labelled for loop|
|Java Nesting of loops|
|Java Break and Continue|
|Java Keywords Dictionary|
|Java Interview Questions|
Variables are the most important part of every programming language. They holds the values to use whenever we required in the program.
A variable is an
identifier denotes a storage location used to store a data value or a variable is a basic unit of storage. Unlike constants thay remain unchanged during the execution of a program. A variable may take different values at different times during the execution of the program.
A variable name can be selected by the programmer in a meaningful way so as to reproduce data stored in it.
Example - radian, class_value, max, PI, total_no_of_days etc
Example - int roll_number, String name, float PI etc;
Java allows us to declare variables as the programmer want. But they must follow some rules before declaration as shown below
|No||Rule||Incorrect way||Correct way|
|1||They must not begin with a digit||0value, 18a, 5days||value0, a18, days5|
|2||Uppercase and lowercase are distinct||int abc;
ABC = 20;
abc = 20;
|3||White spaces or commas are not allowed between variables.||int my value;
|4||It should not be a keyword||int class; float return;||int class1 or CLASS;
float return_ or return1;
|5||The first character must be an alphabet or underscore only.||int @a;
|6||Variable names can be of any length||int ; char ;||int my_first_val;
data typesof the data hold by variable.
scopeof the variable.
int a; float pi , percentages, radius; double distance;
The process of giving
initial values to the variables is known as the
initialization. The variables are
automatically initialized to
zero if into assigned to any other value. A variable must be given a value after it has been declared but
before it is used in an expression. It can be done in following ways.
|1||By assignment statement||Int a;
a = 100;
|2||By calling method||Int a;
a = Integer.parseInt(arg)
Every variable has default value in java. If variable is not initialized when it is first created, java provides default value to that variables
automatically as shown below.
|Type of variable||Variable default value|
During execution of java program the values of variables are changed. But the constant variables which are declared as final cannot be changed. They remains as it is until end of the program.