wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Equals and == Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Equals and ==" Watch "Equals and ==" New topic
Author

Equals and ==

Aparna Misri
Ranch Hand

Joined: Sep 30, 2008
Posts: 39
Hi All,

I have a question related to 'equals' and '=='
In the below example

s1 = new String("abc");
s2 = new String("abc");

if(s1.equals(s2))
System.out.println("s1.equals(s2) is TRUE");
else
System.out.println("s1.equals(s2) is FALSE");

if(s1==s2)
System.out.printlln("s1==s2 is TRUE");
else
System.out.println("s1==s2 is FALSE");

We will getthe output as TRUE as the 'equals()' method check for the content equivality.

Now we will get the FALSE as output because both s1 and s2 are pointing to two different objects even though both of them share the same string content.

What will happen if I write

String s1 = "abc";
String s2 = "abc";

instead of

s1 = new String("abc");
s2 = new String("abc");

Can anyone explain me what will be the answer and why.

Thanks
Aparna
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

This article may interest you.


[My Blog]
All roads lead to JavaRanch
Aparna Misri
Ranch Hand

Joined: Sep 30, 2008
Posts: 39
Thanks Christophe!!!

For 'Strings' I have understood but what will happend in the below case

Object obj1 = 2;
Object obj2 = 2;

if(obj1.equals(obj2))
System.out.println("obj1.equals(obj2) is TRUE");
else
System.out.println("obj1.equals(obj2) is FALSE");

if(obj1==obj2)
System.out.println("obj1==obj2 is TRUE");
else
System.out.println("obj1==obj2 is FALSE");
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
Aparna,

Look into this link and see if it answers your question related to 2 Object assignments.
This link does not directly answer your question, but indirectly, yes it does.
Also, replace the value of obj1 and obj2 to 128 or above in your code and see what happens...

http://www.coderanch.com/t/417781/java-programmer-SCJP/certification/Why-Code-Shows-Error
[ November 17, 2008: Message edited by: Rekha Srinath ]
Aparna Misri
Ranch Hand

Joined: Sep 30, 2008
Posts: 39
Thanks Rekha

It means that Object obj1 = 2;
creates a reference obj1 which points to the address where 2 was stored
and in case of Object obj2 = 2;
since 2 already exist in the memory, so reference obj2 point to the same address in memory.

Right??
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
s1 = new String("abc");
s2 = new String("abc");

Will create two new instances.

if(s1.equals(s2))
System.out.println("s1.equals(s2) is TRUE");
else
System.out.println("s1.equals(s2) is FALSE");

==> TRUE [equals() method is overridden in String class and hence TRUE]

if(s1==s2)
System.out.printlln("s1==s2 is TRUE");
else
System.out.println("s1==s2 is FALSE");

Since s1 and s2 are two different instances the result is FALSE.


s1 = "abc";
s2 = "abc";

This will create/point to the same instance of abc which is from the string pool. Hence s1 and s2 will point to the same instance of abc from the string pool.

Hence

if(s1.equals(s2))
System.out.println("s1.equals(s2) is TRUE");
else
System.out.println("s1.equals(s2) is FALSE");

==> TRUE [equals() method is overridden in String class and hence TRUE]
Irrespective of the above explanation.

if(s1==s2)
System.out.printlln("s1==s2 is TRUE");
else
System.out.println("s1==s2 is FALSE");


Since s1 and s2 will now point to the same instances the result is TRUE.


SCJP, SCWCD, SCBCD
Aparna Misri
Ranch Hand

Joined: Sep 30, 2008
Posts: 39
Thanks Deepak!!! I got it...
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
It means that Object obj1 = 2;
creates a reference obj1 which points to the address where 2 was stored
and in case of Object obj2 = 2;
since 2 already exist in the memory, so reference obj2 point to the same address in memory.


Aparna,
By the code Object obj1 = 2;, you are actually doing boxing...Here, you are converting a primitive (int) to wrapper (Integer) which returns an Integer reference pointing to value 2...That is why you are able to assign this Integer reference to an Object reference, because Integer is an Object. And == and equals() work accordingly.

== ---> returns true if obj1 and obj2 point to the same object.
equals() ---> returns true if obj1 and obj2 values are meaningfully equal.

And the reason why you get false for == for values greater than 127, you can find in K&B, and in the link that I provided.

Hope I am clear.
Aparna Misri
Ranch Hand

Joined: Sep 30, 2008
Posts: 39
Thanks Rekha ,I got it.
karthick devaraj
Greenhorn

Joined: Nov 17, 2008
Posts: 6
hai
== operator which checks only addresses of two different objects while equals() methods checks values in the objects. the equals() method more sensible than the == operator
 
Don't get me started about those stupid light bulbs.
 
subject: Equals and ==
 
Similar Threads
toString Question
== & equals()
String problem
String equals question
(s1==s5) Vs s1==s5