Autoboxing & Transitivity

This has been written about by others but I’m going to comment on it as I did run into it tonight.

I’ll start by saying that I sit on the fence with regards to the use of autoboxing in java5. Normally I think it’s alright but I did run into a situation this evening where the transitivity properties of equality didn’t hold as I expected (well I expected them to behave as they did, but it did run slightly counter to the definition of transitivity depending on how the code was written).

Code:
<br /> public class Test {</p> <p>public static void main(String[] args)<br /> {<br /> Long int1 = new Long(1);<br /> long int2 = new Long(1);<br /> Long int3 = new Long(1);</p> <p> System.out.println(int1 == int2);<br /> System.out.println(int2 == int3);<br /> System.out.println(int1 == int3);<br /> }</p> <p>}
Output:
<br /> true<br /> true<br /> false<br />

However, if we swap 1L in for ‘new Long(1)’, than transitivity holds and A == B && B == C && A == C.

Nothing more than food for thought.

It’s almost too easy to unconsciously make use of autoboxing so we should weigh the benefits of simplicity with the potential for some unexpected results.