This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
Yikes! The equals() method seems to be working, but what happened with == and != ? Why is != telling us that i1 and i2 are different objects, when == is saying that i3 and i4 are the same object? In order to save memory, two instances of the following wrapper objects will always be = = when their primitive values are the same:
Character from \u0000 to \u007f (7f is 127 in decimal)
Short and Integer from -128 to 127
Waiting for a favorable reply. Kind Regards. Hasnain Javed Khan.
If the object of a wrapper is within the size of a byte and you are boxing, only one objects is made and this goes into the wrapper's pool. The other variable refers to the same object then and so == indicates that they are the same object.
For numbers extending the range of a byte such a pooling is not possible (remember - there are a lot of different numbers out there ).
But as I said already this works only with boxing.
Integer a = new Integer(10); Integer b = new Integer(10); System.out.println(a==b); // false Integer c = 10; Integer d = 10; System.out.println(c==d); // true Integer big1 = 1000; Integer big2 = 1000; System.out.println(big1==big2); // false
prints what's indicated in the comments.
== only indicates if the objects are the same, not the value. equals indicates if they are meaningfully equal. Would be all true with the example.
Will be back in january, Bu.
Joined: Dec 15, 2007
Hi Burkhard, Thanks for the explanation . Its clear now. Kind Regards. Hasnain Javed Khan.