== symmetrical ; those. for any values ββof x and y , (x == y) == (y == x) . This is the guarantee provided to us by JLS Β§15.21.1 for numbers, and Β§15.21.3 for reference types (or anything that is not a primitive value).
It can also be considered as transitional , if there are three values x, y, z and x == y && y == z , then x == z . This is again provided by the same JLS specification - just repeated to reduce the problem with the common variable y .
A real problem arises regarding autoboxing; when you go to unbox null , then using JLS , you will get a NullPointerException - regardless of the comparison operation that you are "Next."
Effectively:
You have the primitive boxed type on the one side of the comparison, and the primitive on the other. The value has not yet been considered.
Given that the primitive value will force a numerical comparison because it is a boxed primitive, Java will then try to unpack the value in the box.
You cannot unpack null , hence NullPointerException .
This is (the view) where equals() takes effect - under his contract, two non-empty instances must be equivalent to each other if they are really the same. If either (but not both) of these values ββis null , then they are not the same instance.
I say "kinda" because Object#equals really have nothing to enforce the intended contract; you could (with some effort) write the asymmetric equals() method, although you might wonder why you would like to.
Makoto
source share