Thats because, Integer.equals() methods, test the primitive value of the integers, if the both Integer object's primitive values are same, then method returns true.
For '=' comparision, you are directly comparing object's references, java fundamentals, two different objects are not same, though its values are same, they are different objects in memory.
In addition to the above explanation please refer to the following article. The first couple of paragraphs give you the answer but if you bear few more minutes then it will help you to clear this point.
Note: I personally suggest reading this article fully because it will help you in collections API. [ October 12, 2007: Message edited by: Mayur Ramgir ]
following wrapper objects will always be == when their primitive values are the same: 1. Boolean 2. Byte 3. Character from \u0000 to \u007f (7f is 127 in decimal) 4. Short and Integer from -128 to 127
Originally posted by Thirumalai Muthu: Integer i1= 1000; Integer i2= 1000; if(i1!=i2){System.out.println("different objects"); if(i1.equals(i2)) { System.out.println("Same objects"); }
produces the O/P
different objects Same objects
Can anyone Please Explain.
Can you assign a literal like that? ie. Integer i1 = 1000? Shouldnt it be something like Integer i1 = new Integer(1000)? [ October 12, 2007: Message edited by: O. Ziggy ]