Connecting Tech Pros Worldwide Help | Site Map

what is the use of String.intern()

  #1  
Old October 13th, 2007, 11:18 AM
Member
 
Join Date: Oct 2007
Location: Velacheri in Chennai(INDIA)
Posts: 75
what is the use of String.intern(). Is there any difference between a copy of a string object and the return value of this function ?
This function also returns a copy of the string which it acts on. Clear me please.
  #2  
Old October 13th, 2007, 12:07 PM
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,634
Provided Answers: 2

re: what is the use of String.intern()


Quote:
Originally Posted by pjerald
what is the use of String.intern(). Is there any difference between a copy of a string object and the return value of this function ?
This function also returns a copy of the string which it acts on. Clear me please.
When you use literal Strings in your program (and most likely you do), the javac
compiler collects those strings. When you want to run your program those literal
Strings are stored in a String pool. It stores just unique Strings in that pool, so:

Expand|Select|Wrap|Line Numbers
  1. String s1= "foo";
  2. String s2= "foo";
  3.  
This takes up one String in that pool: "foo" and both pointers s1 and s2 point to
that single String. You can put Strings in that pool too by using the String.intern()
method:

Expand|Select|Wrap|Line Numbers
  1. String s1= new String("foo");
  2. String s1= new String("foo");
  3. s1= s1.intern();
  4. s2= s2.intern();
  5.  
After the initialization s1 and s2 point to two different String instances, both
having the value "foo". The literal String "foo" used for the instantiation of the
Strings is stored in the String pool. After the initialization there are *three* strings
"foo".
After interning both Strings in the last two statements there's still one String "foo"
in the String pool (because it already was there) and both references s1 and s2
point to it. The two other String instantiations (that also contained "foo" but not
the same as the one in the pool) can be garbage collected because nothing points
to them anymore.

When cleverly used that pool can save you quite a bit of memory.

kind regards,

Jos
  #3  
Old October 13th, 2007, 12:36 PM
Member
 
Join Date: Oct 2007
Location: Velacheri in Chennai(INDIA)
Posts: 75

re: what is the use of String.intern()


Thanks a lot Jos. This is the first time I heared about the String pool and the particular job of the compiler regarding String.
Thanks,
P.Jerald.
  #4  
Old October 13th, 2007, 01:50 PM
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,634
Provided Answers: 2

re: what is the use of String.intern()


Quote:
Originally Posted by pjerald
Thanks a lot Jos. This is the first time I heared about the String pool and the particular job of the compiler regarding String.
Thanks,
P.Jerald.
You're welcome of course; I should've mentioned one slight restriction: the Strings
in that pool must have a length < 64KB; the bytes contain the UTF8 encoded
version of the String. So if a String contains non-ASCII characters the maximum
length is even less. (see the JVM specification (sorry, too lazy to search for a link ;-)

kind regards,

Jos
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
Use of Return Statement jamil@onepost.net answers 8 August 9th, 2007 11:45 PM
String Interning and Thread Locking Chris Mullins answers 5 November 15th, 2006 02:15 AM
Intern Strings - am I using them right? Please help! almurph@altavista.com answers 2 January 24th, 2006 04:55 PM
how to make two references to one string that stay refered to the same string reguardless of the changing value in the string? Daniel answers 10 November 16th, 2005 02:34 PM