2
2
.
.
3
3
.
.
2
2
V
V
a
a
r
r
i
i
a
a
b
b
l
l
e
e
s
s
I
I
n
n
f
f
o
o
Variable has value which can be changed any number of times (unlike Constants which always hold initial value).
Referencing Variable that has no Value gives error: the local variable 'age' might not habe been initialized.
This is because only Optional Variables are allowed to be null. This is why Optionals were introduced in the first place to
detect these kind of errors at compile time rather than during run time.
V
V
a
a
r
r
i
i
a
a
b
b
l
l
e
e
S
S
y
y
n
n
t
t
a
a
x
x
Variable is declared by
providing required Variable's Name name
followed by required Variable's Data Type String
followed by optional Variable's Value "John" (can also be set later multiple times)
E
E
x
x
a
a
m
m
p
p
l
l
e
e
In this example we declare
Variable name by specifying Data Type as String and by giving it an initial value of "John"
Variable age by specifying Data Type as Int since no initial Value was given from which Type could be inferred
Variable weight by omitting Data Type since it can be inferred as Int from the given initial Value of 180
Variable Syntax
//DECLARE VARIABLE.
String name = "John"; //Full Syntax with both Data Type and initial value
int age; //Only Data Type without initial value
//ASSIGN NEW VALUE TO VARIABLE.
age = 20; //Constant's value can be set only once
//age = 30; //This line would throw error since we are trying to change value of a
Constant
//DISPLAY VARIABLE.
System.out.println(age);