Variables
In computer science, we think of a variable as a storage location that holds a data value. In Java, a variable is introduced by declaring it to have a specific type. For example, consider the following statement:
String lastName;
It declares the variable lastName
to have type String
.
We can also initialize a variable with an explicit value when it is declared, like this:
double temperature = 98.6;
Here, we would think of a storage location named temperature
that contains the value 98.6
and has type double
.
Structured variables can also be declared and initialized in the same statement:
int[] a = {88, 11, 44, 77, 22};
This declares the variable a
to have type int[]
(array of ints
) and contain the five elements specified.