1 Evaluating integer expressions
The right side of an assignment operator (equal sign) can be any expression — any combination of numbers, variables, and mathematical operators (+
, 
, *
, /
).
You can also use parentheses ()
. In Java, as in mathematics, expressions inside parentheses are evaluated first, and then what is outside the parentheses.
Multiplication and division have equal precedence and are higher than addition and subtraction.
Examples:
Statement  Note 


The value of the variable will be 8 

The value of the variable will be 1 

The value of the variable will be 6 

Executing this statement will produce a "division by zero" error, and the program will terminate. 
An expression can also include variables:
Statement  Note 


The value of the variable a will be 1 The value of the variable b will be 2 The value of the variable c will be 4 
What's more, the same variable can be on both the left and the right of the assignment operator:
Statement  Note 


The value of the variable x will be 5 The value of the variable x will be 6 The value of the variable x will be 7 The value of the variable x will be 8 The value of the variable x will be 9 The value of the variable x will be 10 
The point here is that in Java the =
symbol does not mean equality. Instead, it is an operator that assigns to the variable on the left of the =
sign the calculated value of the expression to the right of the =
sign.
2 Division of integers
In Java, dividing an integer by an integer always results in an integer. The remainder of the division operation is discarded. Or, you could say that the result of division is always rounded down to the nearest integer.
Examples:
Statement  Result of division  Note 


2.5 
The value of the variable will be 2 

6.3333(3) 
The value of the variable will be 6 

1.2 
The value of the variable will be 1 

0.5 
The value of the variable will be 0 
3 Remainder of division of integers
Besides addition, subtraction, multiplication, and division of integers, Java also has the modulo operator. It is the percent symbol (%
). This operator returns the whole number remainder of dividing an integer by an integer (not the fractional part).
Examples:
Statement  Result of division  Note 


2 with a remainder of 1 
The value of the variable will be 1 

5 with a remainder of 0 
The value of the variable will be 0 

1 with a remainder of 4 
The value of the variable will be 4 

0 with a remainder of 1 
The value of the variable will be 1 
This is a very useful operator. It is used a lot. For example, to find out whether a number is even or odd, just divide it by 2
and compare the remainder with zero. If the remainder is zero, then the number is even; if it is equal to one, then the number is odd.
Here's what this check looks like:
(a % 2) == 0
where, you guessed it, a % 2
is the remainder of division by 2
(i.e. 0
or 1
), and ==
is used to compare with zero.
4 Increment and decrement
In programming, increasing or decreasing a variable by one are very common operations. There are special commands for these actions in Java:
The increment (increment by one) operator looks like this:
a++;
This statement is exactly the same as a = a + 1;
It increases the variable a
by one.
The decrement (decrement by one) operator looks like this:
a;
This statement is exactly the same as a = a  1;
It decreases the variable a
by one.
Examples
Statement  Note 


The value of the variable x will be 5 The value of the variable x will be 6 The value of the variable x will be 7 The value of the variable x will be 8 The value of the variable x will be 9 The value of the variable x will be 10 

The value of the variable x will be 5 The value of the variable x will be 4 The value of the variable x will be 3 The value of the variable x will be 2 The value of the variable x will be 1 The value of the variable x will be 0 The value of the variable x will be 1 
GO TO FULL VERSION