aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Difference between '==' & equals() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Difference between Watch "Difference between New topic
Author

Difference between '==' & equals()

Somesh Rathi
Ranch Hand

Joined: Apr 27, 2006
Posts: 36
What is the difference between '==' and equals() when comparing objects?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Sep 28, 2004
Posts: 1228
== 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 ]

Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
Rushikesh Wagh
Ranch Hand

Joined: Jun 18, 2005
Posts: 58
== Compares Two Values.
equals() Compares Two Object .


Best Regards,
Rushikesh Wagh
SCJP 1.4 (2005)
SCJP 1.6 (2010)
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11229
    
  16

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".


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Somesh Rathi
Ranch Hand

Joined: Apr 27, 2006
Posts: 36
Hi all,
Thank you very much for clarification.I got it!
Once again Thanks a lot!
avi aro
Greenhorn

Joined: Apr 13, 2006
Posts: 16
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

Joined: Feb 02, 2006
Posts: 2023
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

Joined: Aug 31, 2004
Posts: 11343

"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


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Somesh Rathi
Ranch Hand

Joined: Apr 27, 2006
Posts: 36
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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Apr 13, 2006
Posts: 16
Hi Marc,
I have also changed my name. I hope it is fine as well.
Thanks,
Avi
Hareendranath Babu Kotha
Ranch Hand

Joined: May 22, 2006
Posts: 40
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)


Hari.K,<br />+919886082889
Amit Biswas
Ranch Hand

Joined: Jun 01, 2006
Posts: 52
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

Joined: Dec 01, 2005
Posts: 15
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

Joined: May 22, 2006
Posts: 40
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);

}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between '==' & equals()