Originally posted by Carlos Gomez:
Because the Integer class override the equals method and certain primitives are always to be boxed into the same immutable wrapper objects. These special values are:
The boolean values true and false
The byte values
The char values in the range '\u0000' to '\u007F'
The short and int values between -128 and 127
for instance:
Integer j1 = 148;
Integer j2 = 148;
System.out.println(j1 == j2); // FALSE
Integer k1 = 126;
Integer k2 = 126;
System.out.println(k == k); // TRUE
Carlos' reply is almost irelevant to the question - for two reasons:
a) Note that
Integer i = 24;
is NOT same as
Integer i = new Integer(24);
Whatever Carlos has pointed out (special cases etc) holds for the former case, not in the later case.
b) Carlos talks about "==", which is different than what Xiao is talking about - the equals() method.
Coming back to the question posted by Xiao, The class Integer overrides "equals" method so that any two integers with the same numeric value are considered "equal". As a result, see that
Integer i = new Integer(344);
Integer j = new Integer(344);
System.out.println(i.equals(j)); //true.
On the other hand, the class Value doesnot override the equals() method. The equals() method, as defined in the "Object" class returns the same result as "==". Since two different value instances are never "==", hence the equals() method will return false.