• 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

Concept of intern

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I understand if you create two strings with same literals. It put inside a pool but refer to one. It resuses it. For instance,
String str1="test";
String str2="test";
Inside a pool, there will be one test pointed by both str1 and str2.
What happens in runtime and why we need intern? Any help with code explanation would help me a lot. Thanks in advance.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String str1="test";
String str2="test";

Inside a pool, there will be one test pointed by both str1 and str2.

String str3 = new String( "test" );
creates a new String on the heap.
str1 == str3 is false.

String str3 = new String( "test" ).intern() ;
puts that string into the pool.
str1 == str3 is true.
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Method intern is used to see if two strings are the same.
It is used for long text, not short strings.
Jvm guarantee that intern() will return the same reference for each strings having the same value.
So if you want to know if two strings have the same text inside, you can use it.
For e.g:
String mystring= new string("some long text");
String mystring2= new string("some long text");
At this point if you do mystring==mystring2
it will return false, because there is two differents objects, but it doesn't mean that they do not have the same text inside.
After using intern() if == returns false, it does mean that they don't have the same text.
If it returns true they do have the same text.
Sorry for my english.
I hope it helped you!!!
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is useful to intern strings that are going to be compared many times because the comparision with == is quicker than with equals.
 
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Marilyn deQueiroz:
String str3 = new String( "test" ).intern() ;
puts that string into the pool.
str1 == str3 is true.


So, when intern() puts the new version of "test" into the pool, does it sort of knock out the old/other version of "test" that was there before and reset str1 and str2 to point to that new version? (If so, do strings in the pool have handles to whatever references them?)
 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
mike, as i see it now s1,s2,s3 are all pointing to the same object. (all == amongst the 3 references would be true)

Originally posted by Michael Matola:

So, when intern() puts the new version of "test" into the pool, does it sort of knock out the old/other version of "test" that was there before and reset str1 and str2 to point to that new version? (If so, do strings in the pool have handles to whatever references them?)

 
Michael Matola
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by mark stone:
mike, as i see it now s1,s2,s3 are all pointing to the same object. (all == amongst the 3 references would be true)


Right. I wasn't really questioning that. I was wondering which object they now point to --or, in other words, whether the string in the pool (that all 3 references now point to) after interning is the one that was there before or the newly created one.
Just checked the API for intern(), which seems to imply that the old one stays in the pool and the new one gets discarded:


When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic