Can somebody explain me the difference between equals() and == when comparing object references.
Regards
Nikhil Bansal
Srinivasa Raghavan
Ranch Hand
Joined: Sep 28, 2004
Posts: 1228
posted
0
equals() method can be considered to compare values of an object , but == is used to compare just the reference and not the object being refrerred
a.equals(b) is true since both has the same value but a == b is not. Because these two are seperate instances. [ March 21, 2006: Message edited by: Srinivasa Raghavan ]
equals() is a method inherited from Object and can be override For example in the class String the method equals compare the content of two strings
== compare the value of two references (for objects), well the comparison is on the address that refer on the object
I hope that this will help you
Nikhil Bansal
Greenhorn
Joined: Mar 21, 2006
Posts: 21
posted
0
Thanks Valentin and Srinivasa.
Best Regards
Nikhil
Sandeep Vaid
Ranch Hand
Joined: Feb 27, 2006
Posts: 390
posted
0
I hope they have cleared you doubt. Just to add a complexity to this.
Sometimes it may happen that both references are pointing(referencing) to the same location and hence both == and equals() returns true.
Nikhil Bansal
Greenhorn
Joined: Mar 21, 2006
Posts: 21
posted
0
Yes Sandip,
I just read the concept of String Pooling.
Thanks
Nikhil
Valentin Jacquemin
Ranch Hand
Joined: Feb 15, 2006
Posts: 35
posted
0
Do you know that it's possible that the two tests return true for Integer?
This is true for number between -128 and 127 I belive [ March 23, 2006: Message edited by: Valentin Jacquemin ]
Shaliey Gowtham
Ranch Hand
Joined: Mar 20, 2006
Posts: 104
posted
0
Hi Valentin, I think you might be wrong. Only the autoboxed Wrapper Objects for Boolean(true,false), Byte, Character('\u0000' -> '\u--7f', Short( -128 -> 127) Integer( -128 -> 127) Long( -128 -> 127) are pooled. However the Float and Double are not pooled.
But when you create Objects using the new() then they are never pooled as the same as Strings.
sri latha
Greenhorn
Joined: Mar 03, 2006
Posts: 27
posted
0
Hi Valentin Jacquemin when i execute the above code the equals method returns true.which situations can the above code returns true in both == and eqauls method.can u explain it.
[fixed code formatting - Jim] [ March 23, 2006: Message edited by: Jim Yingst ]
ashok ch
Greenhorn
Joined: Feb 21, 2006
Posts: 11
posted
0
Hi,
I executed Valentin Jacquemin's code .
"==" returns false and equals() method returns true;
"==" always compares the references and here i1 and i2 are two different references so the result is "false"
By default, equals() method also compares the references not the state of the objects.But if the equals() method is overriden in the compared classes then the method compares the states of the objects. Integer class has the equals() method overridden in it.That's why the equals() method in this case compares the states of the i1 and i2, which is same, So the result is "true".
I hope it clears..
Thanks Ashok
Valentin Jacquemin
Ranch Hand
Joined: Feb 15, 2006
Posts: 35
posted
0
Ok sorry...
I didn't test yet but in the K&B book it said that I said above... I'll read one more time this evening
Sorry
rajeshwar Rao
Ranch Hand
Joined: Mar 20, 2006
Posts: 31
posted
0
which situations can the above code returns true in both == and eqauls method.can u explain it.
I think the below code might solve your question. Because whenever you create an object with new it is created with new reference. Hence i1 == i2 fails
public class Test { public static void main (String args []) { Integer i1 = new Integer(8); Integer i2 = i1; if(i1 == i2) { System.out.println(" == return true"); } if(i1.equals(i2)) { System.out.println(" equals() return true"); } } }
Please correct me if i am wrong
gaurav singhal
Ranch Hand
Joined: Nov 18, 2005
Posts: 135
posted
0
As said by shailley But when you create Objects using the new() then they are never pooled as the same as Strings.
But when we use new keyword, Java will create a new String object in normal (non-pool) memory, and s will refer to it. In addition, the string literal will be placed in the pool.
I guess that is same for Integer also. Plz correct me if I am wrong
Valentin Jacquemin
Ranch Hand
Joined: Feb 15, 2006
Posts: 35
posted
0
K&B book on page 236:
Output:
So it was for the autoboxed wrapper object.
Sorry and thanks to Shaliey G
[ March 24, 2006: Message edited by: Valentin Jacquemin ] [ March 24, 2006: Message edited by: Valentin Jacquemin ]