• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Diff between equals() and ==

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Can somebody explain me the difference between equals() and == when comparing object references.

Regards

Nikhil Bansal
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Valentin and Srinivasa.

Best Regards


Nikhil
 
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Sandip,

I just read the concept of String Pooling.

Thanks

Nikhil
 
Valentin Jacquemin
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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 ]
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Clowns were never meant to be THAT big! We must destroy it with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic