This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Using == with Strings

 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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").
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes different objects because Strings are immutable and a new Object is created.
 
Paulo Aquino
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm...I'm a little bit confused... Isn't x1 and x2 now refers to "abc"?
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks!
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



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 ]
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hii Pete

very nicely explained
 
Paulo Aquino
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Pete! Nicely done. It cleared my doubts.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic