![]() |
![]() |
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 Programs | |
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; |
int abc; abc = 20; |
3 | White spaces or commas are not allowed between variables. | int my value; double max,1; |
int myvalue; double max,max1; |
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; float $profit; |
int Alpha; char _data; |
6 | Variable names can be of any length | int ; char ; | int my_first_val; char this_varialbe_for_single_value; |
compiler
data types
of the data hold by variable.scope
of the variable.type variable_name;
OR
type variable1,variable2,variable3;
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.
No | Method | Example |
---|---|---|
1 | By assignment statement | Int a; a = 100; |
2 | By calling method | Int a; a = Integer.parseInt(arg[0]) |
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 |
---|---|
byte | 0 |
short | 0 |
int | 0 |
char | NULL |
float | 0.0f |
double | 0.0d |
long | 0L |
Boolean | false |
Reference | NULL |
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.