• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java String pool

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's the output of following code


the answer is false!
String pool will be checked before add a new string into the pool. So s2 s3 should refer to the same String object?
 
stable boy
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are using the reference equality operator to compare if 2 references are pointing to the same object, you are not comparing the contents of the objects with this operator.
The result of == is true if the operand values are both null or both refer to the same object or array; otherwise, the result is false.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Might I recommend this bit of light reading?
 
ming wu
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

is same as
[/qb]<hr></blockquote>

Not sure what your question is here but keep in mind the following:
The == operator is checking to see if the object references point to the same object. The equals method checks to see if the characters in each of the strings are identical. The line:

creates a new string object
Similarily, the line:

creates a new string object.

Thus, when each of the above items are compared using the == operator, the result if false. The equals method would however return true.
[ October 11, 2004: Message edited by: Chris Allen ]
 
ming wu
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by mingwuming mingwu:

is same as

 
ming wu
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks,,it is a typo,,i will becareful next time
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had a similar problem and spent a while figuring this stuff out. You guys are right, but I didn't see you point until I had already figured it out myself. The thing is the difference between using s="string" and s=new String("string").

public void testStringPool(){
String s1 = "foo";
String s2 = "foo";
assertTrue(s1==s2); // *1
String s3 = new String("foo");
assertFalse(s1==s3); // *2
assertTrue(s1.equals(s3)); // *3

/* notes:
* 1: s1 and s2 ARE the same, because getting a string using s="string"
* will actually get the string from Java's String pool.
* 2: s1 and s3 are NOT the same, because using s=new String("string")
* will (apparently) cause Java to instantiate a new String, and not
* get it from the pool.
* 3: That's why you should always use equals instead of == to compare
* strings if you want to compare the contents of the strings.
*/
}

Pretty dodgy stuff, no?
[ October 27, 2004: Message edited by: Fredrik Petersson ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic