Jason Attin wrote:Now with regard to E, the explanation says
Due to auto-boxing int 42 is converted into an Integer object containing 42. So this is valid. It will return false though because ln is a Long and 42 is boxed into an Integer.
What I'm not entirely sure about is why 42 is boxed to an Integer object. Is that because equals() requires two objects?
42 gets boxed to an Integer object because 42 is an int literal. As for why it happens... it is because there isn't a equals() method that takes an int parameter, so it has to box the int in order to resolve the equals() method.
Jason Attin wrote:
Let's take a different scenario, just so I understand:
this presumably will return true because ln is auto-unboxed to a long and then the integer 42 is promoted to a long, correct? This comparison is possible because "==" doesn't require both the operands to be objects and in fact it will not compile if they were both objects.
Correct. If one side of equality operator is not an object, then unboxing will be used to get both sides to be primitive types.
Henry