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

String.intern() issue

 
Ranch Hand
Posts: 57
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am having small doubt can you please look into the following code snippet.

String s1 = new String("Legend");

String s2 = s1.intern();

System.out.println(s1==s2); //>> It is printing as false

but s1.intern() method will give s1 object reference right. and by using s1==s2 we are comparing two object references but why it is coming false.

 
Ranch Hand
Posts: 199
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mike,

Your new String goes to the heap, say with reference "H23", and when you force it to be placed on the pool gets a diferent reference, say "P44".
If you create a new string s3 "Legend" and force it to the pool then gonna have the same reference "P44".



Best regards,
 
Carles Gasques
Ranch Hand
Posts: 199
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

This thread lead me to a performance question.

Before I could invoke the intern method on a String object, I have to instantiate it, therefore I have spend resources in this step
(I understand that instantiation is a really expensive step) then the String reference from the heap (could be garbage collected) is replaced by a reference from the pool.

From a performance perspective couldn't be more optimal to have an additional static method 'String.intern("Legend")' that makes unnecessary to create the String in the pool.
What do you think?


Best regards,

 
Marshal
Posts: 74637
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the performance overhead is much less than you fear.
Creating new Strings with the copy constructor and then interning is a pointless activity at any time.
 
Campbell Ritchie
Marshal
Posts: 74637
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe not completely pointless, but probably unnecessary most of the time.
 
Carles Gasques
Ranch Hand
Posts: 199
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ritchie for share your point of view.
 
Campbell Ritchie
Marshal
Posts: 74637
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
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