From the JLS:
15.21.1. Numerical Equality Operators == and !=
If the operands of an equality operator are both of numeric type, or one is of numeric type and the other is convertible (§5.1.8) to numeric type, binary numeric promotion is performed on the operands (§5.6.2).
And the relevant rule from 5.1.8 is:
If r is a reference of type Integer, then unboxing conversion converts r into r.intValue()
And 5.6.2 says:
5.6.2. Binary Numeric Promotion
When an operator applies binary numeric promotion to a pair of operands, each of which must denote a value that is convertible to a numeric type, the following rules apply, in order:
If any operand is of a reference type, it is subjected to unboxing conversion (§5.1.8).
Which means that if(foo == 5) ...;
means the same as if(foo.intValue() == 5) ...;
and if(5 == foo)
means if (5 == foo.intValue())
.
If foo
equals null
then you will get an NPE in either case.