wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Using == with Strings 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 "Using == with Strings" Watch "Using == with Strings" New topic
Author

Using == with Strings

Paulo Aquino
Ranch Hand

Joined: Apr 29, 2002
Posts: 200


Ok, this is the way I understand this. There were 3 objects created:
"abc"
"ab"
"c"


At first line, the reference variable x1 refers to "abc", at the second line, the ref var x2 refers to "ab". At the third line, x2 was concatenated with "c" so x2 now refers to "abc". My question is why is it that when x1 != x2 was executed, it returned true? As I understand it, both the reference variable x1 and x2 refers to the same object ("abc").


Be Afraid...Be very Afraid...
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Yes different objects because Strings are immutable and a new Object is created.


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

Joined: Apr 29, 2002
Posts: 200
Hmmm...I'm a little bit confused... Isn't x1 and x2 now refers to "abc"?
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Again going back to the same url . Check that out.

I'm sure that this thread will go a long way like the one in last week.
[ April 07, 2005: Message edited by: Srinivasa Raghavan ]
Paulo Aquino
Ranch Hand

Joined: Apr 29, 2002
Posts: 200
Thanks!
Saurabh Khanna
Ranch Hand

Joined: Apr 03, 2005
Posts: 30
Two points:

1) a == b is comparing reference values not objects.

2) Strings are immutable, so :

String s = "abc";

String s1 = "a";
String s2 = "bc";

String s3 = s1 + s2; // creates a new String object with value "abc"

So, s and s3 are referencing to different objects.

I suggest you use the equals() or equalsIgnoreCase() method to compare
strings..

- Saurabh
Pete Knecht
Ranch Hand

Joined: Mar 30, 2005
Posts: 33


Ok, this is the way I understand this. There were 3 objects created:
"abc"
"ab"
"c"


At first line, the reference variable x1 refers to "abc", at the second line, the ref var x2 refers to "ab". At the third line, x2 was concatenated with "c" so x2 now refers to "abc". My question is why is it that when x1 != x2 was executed, it returned true? As I understand it, both the reference variable x1 and x2 refers to the same object ("abc").



Paulo,
To specifically answer your question, x1 and (the reassigned)x2 DON'T refer to the same object, so x1 != x2 holds.

Your example comes from K&B p.384. If you re-read that page and compare it to what they say about strings, literals, and the string pool on pp. 359-360, it makes sense why.

x1 above is a literal, so "abc" gets put in the pool. However,
x2 = x2 + "c"; is not considered a literal, and so it won't match the "abc" literal already in the pool. If you had had, x2 = "abc", then x1 and x2 would refer to the same object in the pool.

Also, suppose you add to your code:
String x3 = new String("abc");

In this case, does x1==x3? No. The object x3 is put in non-pool memory(because of the way it was constructed), so x1 and x3 don't refer to the same object. (But their content(value) is the same, so x1.equals(x3) holds.)

Again, compare those pages I mentioned above, run more tests with an eye to literals, non-literals, and the string pool and it becomes clear.

Pete.
[ April 07, 2005: Message edited by: Pete Knecht ]
Anuj Troy
Ranch Hand

Joined: Apr 07, 2005
Posts: 30
hii Pete

very nicely explained


SCJP 1.4, SCWCD 1.4
your eyes cannot see what your mind does not know
Paulo Aquino
Ranch Hand

Joined: Apr 29, 2002
Posts: 200
Thanks Pete! Nicely done. It cleared my doubts.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Using == with Strings
 
Similar Threads
A String Question
String equals
String Memory Question
string constant pool
About Strings...