Hi all,
The following is a
test script for == and equals() behavior. It's based on an earlier post a found, I just added some cases and a note for each explaining the behavior. I would appreciate feedback on the comments. In particular I'm not sure about b.equals(e)...
Since equals() uses instanceof, I assume that only object type matters and reference variable type is not relevant?
public class TrickyBox {
public static void main(
String [] args) {
Integer a = new Integer(12);
Integer b = 12;
int c = 12;
Object d = new Long(12);
Object e = 12;
Integer f = 128;
Object g = 128;
if (a == b) System.out.println("a == b"); // false (compares Integer objects only one of which was created by autoboxing so the object was not pooled (2 different objects))
if (a == c) System.out.println("a == c"); // true (so 'a' being unboxed)
if (a == d) System.out.println("a == d"); // false (Integer being compared with an Object (2 different objects))
if (a == e) System.out.println("a == e"); // false (")
if (b == c) System.out.println("b == c"); // true (so 'b' being unboxed)
if (b == d) System.out.println("b == d"); // false (object comparison with == (not same object so not == ))
if (b == e) System.out.println("b == e"); // true (so 'b' and 'e' refer to the same object... pooling occurs for values -128 to 127 based on object type and nor reference variable type))
if (c == d) System.out.println("c == d"); // false
if (c == e) System.out.println("c == e"); // true (so 'e' being unboxed)
if (d == e) System.out.println("d == e"); // false (different objects)
if (f == g) System.out.println("f == g"); // false (objects not pooled since above 127)
if (a.equals(b)) System.out.println("a and b are equals"); // true (equal values and both are Integers))
if (a.equals(c)) System.out.println("a and c are equals"); // true (c boxed)
if (a.equals(d)) System.out.println("a and d are equals"); // false (can't widen)
if (a.equals(e)) System.out.println("a and e are equals"); // true (e boxed)
if (b.equals(c)) System.out.println("b and c are equals"); // true (c boxed to Integer)
if (b.equals(d)) System.out.println("b and d are equals"); // false (can't widen)
if (b.equals(e)) System.out.println("b and e are equals"); // true (b and e boxed??)
// compile error since c is an int and has no equals method !
// if (c.equals(d)) System.out.println("c and d are equals");
// if (c.equals(e)) System.out.println("c and e are equals");
if (d.equals(e)) System.out.println("d and e are equals"); // false (can box e to Integer but can't widen to Long))
// compile error because you can't cast an Integer as a Long (not in the same hierarchy)
// if (a.intValue() == ((Long)b).longValue()) System.out.println("EQ ab");
if (a.intValue() == ((Long)d).longValue()) System.out.println("EQ ac"); // true
if (a.intValue() == ((Long)d).longValue()) System.out.println("EQ ac"); // true
if (new Integer(2).equals(new Integer(2))) System.out.println("equals new"); // true
}
}
[ January 07, 2007: Message edited by: Paul Lachance ]