Declaring Variables
To use any variable in a Java program, we must first declare it. Variable declarations consist of a type and a variable name:
int myAge;
String myName;
boolean isTired;
Variable definitions can go anywhere in a method definition (that is, anywhere a regular Java statement can go), although they are most commonly declared at the beginning of the definition before they are used:
public static void main (String args[]) {
int count;
String title;
boolean isAsleep;
…
}
We can string together variable names with the same type on one line:
int x, y, z;
String firstName, LastName;
We can also give each variable an initial value when we declare it:
int myAge, mySize, numShoes = 28;
String myName = “eBIZ”;
boolean isTired = true;
int a = 4, b = 5, c = 6;
If there are multiple variables on the same line with only one initializer the initial
value applies to only the last variable in a declaration.
We can also group individual variables and initializers on the same line using
commas, as with the last example.