In Java, variables are declared as integer using the required word int. In programming terminology, we say that int is a reserved word. Thus, the statement:
int a, b, sum;
‘declares’ that a, b and sum are integer variables. In Java, all variables must be declared before they are used in a program. Note that the variables are separated by commas, with a semicolon after the last one. If we were declaring just one variable (a, say), we would write:
int a;
The statement
a = 14;
is Java’s way of writing the assignment statement
set a to 14
It is sometimes pronounced “a becomes 14”. In Java, an assignment statement consists of a variable (a in the example), followed by an equals sign (=), followed by the value to be assigned to the variable (14 in the example), followed by a semicolon. In general, the value can be a constant (like 14), a variable (like b) or an expression (like a + b). Similarly, “set b to 25” is written as:
b = 25;
and “set sum to a + b” is written as:
sum = a + b;
One final point: the variable sum is not really necessary. We could, for instance, have omitted sum from the program altogether and used:
int a, b;
a = 14;
b = 25;
System.out.printf("%d + %d = %d\n", a, b, a + b);
to give the same result since Java lets us use an expression (e.g. a + b) as an argument to printf. However, if the program were longer and we needed to use the sum in other places, it would be wise to calculate and store the sum once (in sum, say). Whenever the sum is needed, we use sum rather than recalculate a + b each time.