Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

== confusion

 
kavin kumar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 528
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deleted your duplicate post. Please refrain from duplicate posting. Thanks

Mark
 
kavin kumar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai
== USED TO COMPARE REFERENCE VARIABLES REFEREING TO SAME TO CLASS OR NOT
.equals() USED TO COMPARE THE CONTENT OF THE OBJECTS
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic