Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between '==' & equals()

 
Somesh Rathi
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between '==' and equals() when comparing objects?
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually the comparison is between references.

When you use == between two object references, it test whether or not the references point to the same object in memory.

When you use .equals between two object references, then either an overridden equals method is called to determine what the programmer means by the two objects being equal or the equals inherited from Object is called, in which case it is the same as ==.
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
== compares references. "equals" compares content.



In the above code when you use == to compare 'a' and 'b' it will return false since these two are different objects but a.equals(b) will return true since they both have same contents
[ May 05, 2006: Message edited by: Srinivasa Raghavan ]
 
Rushikesh Wagh
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
== Compares Two Values.
equals() Compares Two Object .
 
fred rosenberger
lowercase baba
Bartender
Posts: 12181
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when you have something like

MyClass foo = new MyClass();

the 'foo' is not an object, but a reference to an object. foo basically says "look over here in THIS spot for the real thing".

so when you have two references, foo and bar, the == says "do they both refer to the same spot?"

the equals() method says "use the method defined to figure out if what each one refers to are equilvelant." they may both point to the same bin, in which case the answer is yes.

but if one points to spot A, and the other points to spot B, it's possible that the two different things in those spots are equivilant. they are the same type of object, with the same values in the member variables, etc.

the thing about equals() is that while every class has a version by default (possibly inherited from the Object class, possibly defined somewhere else), you can define your own for classes you create. YOU can decide what makes two instances of your class "equal".
 
Somesh Rathi
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Thank you very much for clarification.I got it!
Once again Thanks a lot!
 
avi aro
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,
I understand that equals() compares values of two string and == compares references. From this
___________________________________________________________________________
String str1 = "A";
String str2 = "A";
System.out.println(str1+" equals() "+str2+" "+(str1.equals(str2))+" == "+(str1 == str2));
___________________________________________________________________________

The output should be A equals A true == false

But the eclipse and netbeans give me output as:
A equals() A true == true.

But if I change this to:
_______________________________________________________________________
String str1 = new String("A");
String str2 = new String("A");
System.out.println(str1+" equals() "+str2+" "+(str1.equals(str2))+" == "+(str1 == str2));
_______________________________________________________________________

The output is
A equals() A true == false

I will like to ask that what is difference here between String str1 = "A" and String str1 = new String("A"). Is a new reference not created when we do str1 = "A"??
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A string is often created from a string literal � a series of characters enclosed in double quotes. The shortcut syntax for instantiating new String objects from string literals:

String s = "A";

This special shortcut syntax was designed to improve String performance: The JVM sets aside a special area of memory called the "String constant pool". Each JVM only keeps one copy of each string literal. If the compiler encounters a String literal, and the String literal has been created in String constant pool, the reference to the new String literal is directed to the existing String.

So
String s = "A";
String s1 = "A";
"A" is in the String pool, the compiler will assign s and s1 to the same String object in String pool.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"somesh" & "avi",

Please revise your display names to meet the JavaRanch Naming Policy. To maintain the friendly atmosphere here at the ranch, we like folks to use real (or at least real-looking) names, with a first and a last name.

You can edit your name here.

Thank you for your prompt attention!

-Marc
 
Somesh Rathi
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Marc,
Apologies for inconvience caused.I have changed my name according to Naming policy.Is it right now ?

Thanks n Best Regards,
Somesh Rathi
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Somesh Rathi:
Hi Marc,
Apologies for inconvience caused. I have changed my name according to Naming policy. Is it right now? ...

Looks good to me. Thank you!
 
avi aro
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Marc,
I have also changed my name. I hope it is fine as well.
Thanks,
Avi
 
Hareendranath Babu Kotha
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
== operator compares two references , where as equals() wii checks contents of the String objects.

Why B'coz , equals() of Object class is overided in String class in such a way that it should compare contents of the Two String objects not tthe references of the two String Objects.

For Example :

String S1="Testing";
String S2="Testing";

if (S1==S2)--------always returns false, where as coming to equals()

if (S1.equlas(S2))-------> always returns True.

This is what i observed and this is the thing that i know, Will you please let me know if you have any other reasons.


A true friend will never hurt.

Ammu(Hari.K)
 
Amit Biswas
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hareendranath,
The example you gave is incorrect:

----------------------------------------------------------------------------
For Example :

String S1="Testing";
String S2="Testing";

if (S1==S2)--------always returns false,
where as coming to equals()if (S1.equlas(S2))-------> always returns True.

This is what i observed and this is the thing that i know, Will you please let me know if you have any other reasons.

----------------------------------------------------------------------------

The reason is that "Testing" is an anonymous String class and the same object is being referenced referenced by S1 and S2.

The equals operator returns true because the both the strings have the same content.

I am not sure if have really observed that the bolded statement (== operator) always returns false.

 
Viv Gupta
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by hareendranath babu Kotha:


For Example :

String S1="Testing";
String S2="Testing";

if (S1==S2)--------always returns false, where as coming to equals()

if (S1.equlas(S2))-------> always returns True.




No, S1==S2 will not always return false. Rather I will say that it will always return true(in this case). Why ? I think wise owen explanation is the exact answer for this.

[ June 12, 2006: Message edited by: Viv Gupta ]

[ June 12, 2006: Message edited by: Viv Gupta ]
[ June 12, 2006: Message edited by: Viv Gupta ]
 
Hareendranath Babu Kotha
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

Yes what you said is correct . Actually i'm creating String Objects
literal way in which it will take same reference,b coz we are not creating it by using new oprator.

But , the below example is obvious to our conversation.i think it is clear.

class StringTest
{
public static void main(String[] args)
{
String s1=new String("Testing");
String s2=new String("Testing");

boolean flag=false;
if(s1==s2)
{
System.out.println(" == ----------------------->true");
System.out.println(flag);
}
else if(s1.equals(s2))
{
System.out.println("equals()-------------------->true");
flag=true;

}
else
{
System.out.println("false");
}

System.out.println(flag);

}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic