Before the questions, make sure you can: predict the result of integer division and the remainder
operator, including with negative operands; apply the precedence and associativity rules without
guessing; explain the difference between i++ and ++i inside an expression;
expand a compound assignment such as b += 5 and say why it hides a cast; decide whether a
conversion is widening (automatic) or narrowing (needs a cast); and trace an expression that
mixes numbers and strings with +.
Arithmetic operators
| Operator | Name | Behaviour |
|---|---|---|
+ - * |
add, subtract, multiply | result type is the wider of the two operand types, and at least int |
/ |
divide | integer division truncates when both operands are integers: 7/2 is 3, -7/2 is -3 |
% |
remainder (modulus) | 7%2 is 1; the sign follows the left operand: -7%2 is -1 |
++ -- |
increment, decrement | add or subtract 1; prefix or postfix |
If either operand is a floating-point value the division is real: 7.0/2 is 3.5. To get
a real result from two int variables, cast one of them: (double) total / count.
Integer division by zero throws an ArithmeticException at run time; floating-point division
by zero does not — it yields Infinity or NaN.
Precedence and associativity
From highest to lowest, the part you need:
()parentheses++ --(unary), unary+ -,!, cast(type)* / %+ -< <= > >=== !=&&then||?:then= += -= *= /= %=
Binary operators of equal precedence associate left to right; assignment associates
right to left. So 2 + 3 * 4 is 14, (2 + 3) * 4 is 20, and
10 % 3 * 2 is 2 (remainder first, then multiply).
Increment, decrement and compound assignment
int i = 5;
int j = i++; // postfix: j gets 5, then i becomes 6
int k = ++i; // prefix: i becomes 7, then k gets 7
int m = 5;
m = m++; // still 5: the old value is stored back over the increment
Compound assignment operators perform an implicit narrowing cast, which is why the first line below compiles and the second does not:
byte b = 10;
b += 5; // legal: equivalent to b = (byte)(b + 5)
b = b + 5; // error: b + 5 is an int and cannot be assigned to a byte
Also note that x *= 2 + 3 means x = x * (2 + 3): the whole right-hand side is evaluated
first.
Conversion between types
Widening (automatic, no information lost):
[
byte short int long
float double, char int
]
Narrowing (explicit cast required, information may be lost):
double d = 9.99;
int n = (int) d; // 9 -- truncation toward zero, never rounding
int m = (int) -9.99; // -9
char c = (char)('a' + 1);// 'b'
System.out.println('a' + 1); // 98 -- char is promoted to int
In any arithmetic expression, byte, short and char are promoted to int
first. That is why short a = 1, b = 2; short c = a + b; does not compile: a + b is an
int. boolean takes part in no conversion at all — it cannot be cast to or from a
number.
Integer types wrap around silently on overflow, and floating-point values are
approximations: 0.1 + 0.2 is not exactly 0.3, so never compare doubles with ==;
compare the absolute difference against a small tolerance.
Strings and +
+ is evaluated left to right. As soon as one operand is a String, the operator becomes
concatenation and everything to the right is converted to text:
System.out.println(1 + 2 + "3" + 4 + 5); // 3345
System.out.println("Total: " + 5 + 3); // Total: 53
System.out.println("Total: " + (5 + 3)); // Total: 8
The conditional (ternary) operator
condition ? valueIfTrue : valueIfFalse is an expression, so it produces a value:
int max = (a > b) ? a : b;. Only the selected branch is evaluated — which matters when a
branch contains ++.
int / intis anint. Cast before dividing, not after.j = i++stores the old value;j = ++istores the new one.- Compound assignment hides a cast; plain assignment does not.
byte,shortandcharare promoted tointin arithmetic.- Once a
Stringappears in a+chain, the rest is concatenation.
double avg = sum / count;with twoints — the division happens before the widening, so the fraction is already lost.(int) 3.99is 3, not 4. UseMath.roundif you want rounding.- Comparing
doublevalues with==. - Writing
x = x++and expectingxto increase.
Ready? Close the notes and practise.
39 questions. Predict the output before you check — that is the skill the exam measures.