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?

  1. If + had more precedence than % then the expression would be equivalent to (4 + 3) % 2, and it would evaluate to 1
  2. If + had less precedence than % then the expression would be equivalent to 4 + (3 % 2), and it would evaluate to 5 (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 << >> >>>
  • << and >> Rotate bits
  • >>>   Inserts 0s from left
Relational < > <= >= instanceof
Equality == !=
Bitwise & ^ |
  • Not the same level: & >  ^ > |
  • Not short circuiting: always evaluate both operands
Logical && ||
  • Not the same level: && > ||
  • Short circuiting: if first operand evaluates to false then the second is not evaluated
Ternary ? :
Assignment = += -= *= /= %= …  

When operators of equal precedence (e.g. * and /) appear in the same expression the rules are:

  1. All binary operators are evaluated from left to right
  2. 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:

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 extends A

b instanceof I true
A

B

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 be boolean (or Boolean)
  • exp2 and exp3 must not be an invocation of a void 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.


Leave a Reply