Here's the JLS explanation
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
If the promoted type of the operands is int
or long
, then an integer equality test is performed.
So the Long
is unboxed to long
. And numeric promotion is applied to int
to make it a long
. Then they are compared.
Consider that case where long
would be "demoted" to an int
, you'd have cases like this
public static void main(String[] args) throws Exception {
long lvalue = 1234567891011L;
int ivalue = 1912277059;
System.out.println(lvalue == ivalue); // false
System.out.println((int) lvalue == ivalue); // true, but shouldn't
}
int
will be promoted to along
value. – Phobos