jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes == confusion 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 "== confusion" Watch "== confusion" New topic
Author

== confusion

kavin kumar
Greenhorn

Joined: Jul 21, 2005
Posts: 22

I had the same kind of question in Kathy sierra book. But i dont understand their explanation. Plz help me with this.

I think (s2 == o ) should return false;
since they r refering to 2 different objects.
Correct me if iam wrong ?Explain me why?

Thanks in advance
Kavin
[ August 16, 2005: Message edited by: Mark Spritzler ]
Marcelo Ortega
Ranch Hand

Joined: May 31, 2005
Posts: 519

When used with object refernces, the == operator checks to see if the references refer to same object. In your case o and s2 refer to the same object because they were made aliases when you did:

Object o = s2;

This made o refer to the object which s2 was refering to at the time, and therefor the equality test will return true.

Remember though, that the operands of the == operator (when comparing references) must be found in the same class heirarchy.

hth, cheers.


SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJD, SCEA/OCMJEA

Live life to an interface, not an implementation!
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Deleted your duplicate post. Please refrain from duplicate posting. Thanks

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
kavin kumar
Greenhorn

Joined: Jul 21, 2005
Posts: 22
Sorry mark !I wont.

Hello marzo,

Still iam not clear. Plz take a look at this eg.

Acc. to this one how do u explain?I dont know how to explain my confusion that's why i wrote this eg.

String s1 = new String("abc");
String s3= "abc";
String s4 = "abc";


if(s3==s4)
{ System.out.println("EQ");
}

if(s1==s3)
{ System.out.println("Not good");
}
if(s1.equals(s3))
{ System.out.println("Good");
}
}
Fes D Gaur
Ranch Hand

Joined: Apr 29, 2005
Posts: 54
When you use the new key word you are creating a new String object here for s1. Then for s2 you are referring to a second object. s3 simply latches onto the same object s2 is referring to because of string pooling. Therefore s1==s2 will be false because they are two different objects. s1.equals(s2) or s1.equals(s3) will return true because now you are comparing the contents of the objects.

Fes
naraharirao mocherla
Ranch Hand

Joined: Aug 16, 2005
Posts: 45
HI Kavin Kumar
according to ur code.

whenver u create a string with new key word.it will be given a memory address.
if u create a string using just String s1= "abc";
then this string will be in a pool.
if u create another string with same contents ,but without new keyword then that string will go the already existing string pool.(where "abc "is there);

So ,
s1==s3 gives "NOT GOOD":// references are not same.
s3==s4 gives "EQ".references are same.

s1.equals(s3) gives "Good".// character by character is exactly the same.


According to ur code

nagaraj raja
Ranch Hand

Joined: Aug 06, 2005
Posts: 36
Hai
== USED TO COMPARE REFERENCE VARIABLES REFEREING TO SAME TO CLASS OR NOT
.equals() USED TO COMPARE THE CONTENT OF THE OBJECTS
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: == confusion
 
Similar Threads
String Problem
Technical Interview Questions
Problem in file transfer
== and equals arent clear yet ... look at this
Equals confusion