aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Concept of intern Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Concept of intern" Watch "Concept of intern" New topic
Author

Concept of intern

Rex Rodriguez
Greenhorn

Joined: Jan 16, 2002
Posts: 21
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.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9033
    
  10
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.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
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!!!


Younes
By constantly trying one ends up succeeding. Thus: the more one fails the more one has a chance to succeed.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
It is useful to intern strings that are going to be compared many times because the comparision with == is quicker than with equals.


SCJP2. Please Indent your code using UBB Code
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1721
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?)
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
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
Ranch Hand

Joined: Mar 25, 2001
Posts: 1721
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.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Concept of intern
 
Similar Threads
String Object creation
Why would this print true?
String question
Is Kathy&Bert book wrong? Or my understanding wrong?
doubt in String