Operator Precedence
Operator precedence is very important. Why? Because there are questions in the exam where the right answer depends on knowing what operation is done before or after another. Take the following expression where two operators come into play:4 + 3 % 2
What’s the result of this expression?
- If
+
had more precedence than%
then the expression would be equivalent to(4 + 3) % 2
, and it would evaluate to1
- If
+
had less precedence than%
then the expression would be equivalent to4 + (3 % 2)
, and it would evaluate to5
(and this is the right answer)
So I’m sorry but you must learn the following table, which gives operators from the most to the least level of precedence (operators in the same row have the same level, except logical and bitwise operators):
Kind | Operators | Important to know |
---|---|---|
Postfix | e++ e-– |
|
Unary | ++e --e + - ~ ! |
|
Multiplicative | * / % |
|
Additive | + - |
|
Shift | << >> >>> |
|
Relational | < > <= >= instanceof |
|
Equality | == != |
|
Bitwise | & ^ | |
|
Logical | && || |
|
Ternary | ? : |
|
Assignment | = += -= *= /= %= … |
When operators of equal precedence (e.g. *
and /
) appear in the same expression the rules are:
- All binary operators are evaluated from left to right
- Assignment operators are evaluated from right to left
Of course, you can use parentheses to override operator precedence and in fact you must use parentheses to make code easier to read and to maintain.
Increment and Decrement Operators
The increment (++e
) and decrement (--e
) operators can be used:
- Before the operand: the expression
e
evaluates to the incremented/decremented value - After the operand: the expression
e
evaluates to the original value
This difference of behavior is only important if you’re using the operator as part of a larger expression:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// Simple increment (it doesn’t matter if applied before or after) int i = 0; ++i; // i = i + 1 = 1 i++; // i = i + 1 = 2 // Increment as part of a larger expression: // i evaluates to 2: prints nothing and then i = i + 1 = 3 if (i++ == 3) System.out.println(i); // i evaluates to 4: prints nothing if (++i == 3) System.out.println(i); |
The instanceof Operator
The instanceof
operator tests if an object is an instance of a class or if it implements a particular interface. Important things to know are:
Given | Expression | Result |
---|---|---|
null is not an instance of anything |
null instanceof A |
It compiles but returns false |
B extends A |
b instanceof A |
true |
A implements I
|
b instanceof I |
true |
A
|
b instanceof A |
Compiler error if B is not A subclass |
Ternary Operator
The ternary operator (exp1 ? exp2 : exp3
) is a shorthand for an if-exp1-then-exp2-else-exp3
statement.
I love this operator and I use it whenever the involved expressions are not too complicated to affect code legibility. To avoid a compiler error:
exp1
must beboolean
(orBoolean
)exp2
andexp3
must not be an invocation of avoid
method
Going Further
In the lesson 5 Java Comparison Subtleties I explain some more things about equality operators that you need to know before trying the exam.
If you feel like diving deeper into operators you can also check the good old Oracle Java Tutorials.