• 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

String.intern

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tell me pls why in following code
String s1=new String("Java");
String s2=new String("Java");
s2=s2.intern();
System.out.print(s1==s2);
the result is FALSE. but in
String ss1="Java";
String ss2=new String("Java");
ss2=ss2.intern();
System.out.print(ss1==ss2);
the result is TRUE.

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

Originally posted by Sergey Elberg:
Tell me pls why in following code
String s1=new String("Java");
String s2=new String("Java");
s2=s2.intern();
System.out.print(s1==s2);
the result is FALSE. but in
String ss1="Java";
String ss2=new String("Java");
ss2=ss2.intern();
System.out.print(ss1==ss2);
the result is TRUE.

Thanks



In the first case, the String s1 is a String object containing Java. You intern s2, but that doesn't affect s1.

In the second case, the String ss1 refers to the String literal "Java" which automatically goes into the pool.
[ April 04, 2006: Message edited by: Keith Lynn ]
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sergey Elberg:
Tell me pls why in following code
String s1=new String("Java");
String s2=new String("Java");
s2=s2.intern();
System.out.print(s1==s2);
the result is FALSE. but in
String ss1="Java";
String ss2=new String("Java");
ss2=ss2.intern();
System.out.print(ss1==ss2);
the result is TRUE.

Thanks




In first case you are using keyword new to create String s1 and s2, this creates 2 different objects in memory. When you do s2.intern() it returns the String from String pool but String s1 is still unchanged, hence it returns false.
In case 2, you are not using keyword new to create s1, so s1 points to string in string pool and s2.intern() returns string from string pool and thus it returns true.
[ April 04, 2006: Message edited by: Jay Ashar ]
 
Sergey Elberg
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks,
I didn't now that
String s1="Java"; and String s1=new String("Java"); differed so strongly.

Tell me pls :
is in this case String s1="Java"; s1 - literal oder Obiect ?

and what others values are held in the pool more ?

Thanks
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't look at this too closely, but before you waste too many brain cells on this question, take out the call to the intern method. The intern method is NOT on the exam! If you still have a valid question, well... carry on
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic