• 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

Tricky String question :intern() method

 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
As RHE says that if you create a String object with 'new' keyword the compiler creates a new instance of the String rather than getting it from pool (if such string is availble) and if you want to release the String object to the pool for reuse, then use intern() method.
But my question is when to use intern () method.
Can anybody explain me.
Regards
Prasad
------------------
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The string pool increases the performance ( faster access time ) and reduces memory overheads by avoiding creation of duplicate strings. If you expect to be creating a same string repeatedly 'n' number of times, it is better to create it once and <code>intern()</code> it so that subsequent calls return you a reference from the pool instead of creating redundant copies on the heap.
Lets say you are writing a program which does some sort of a look up on a set of values that are loaded during initialization. These static string values that you have read into memory can all be <code>intern()</code>ed because you expect to create similar strings during the course of the program. This may not be the best example, but hopefully gives you an idea
Ajith
 
Prasad Ballari
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ajith
I tried with following code,COnceptually i should get "==" and "equals" statements as the s2 is going to use pooled string.But i am getting only "equals".
Can you advice me.
public class StringTest{
public static void main(String[] args){
String s1=new String("javaranch");
s1.intern(); // adding it to pool
String s2="javaranch"; //getting the value from pool
if (s1==s2){
System.out.println("==");
}
if (s1.equals(s2)){
System.out.println("equals");
}
}
}
Regards
Prasad
------------------
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Calling intern() method on a String places the String in the literal pool of Strings if it was not already there. All strings share "unique" instances when intern'd using String.intern() method.

This means if you intern all your strings, then all duplicates are shared and you can do the much faster reference comparison (==) instead of the content comparison (equals() which is slower). Basically, this results in performance optimization.

To give an example :

<code>
String s1 = "Hello"; // Implicitly intern'd
String s2 = new String("Hello"); // Line 2 - Not intern'd as it is a copy of the String object.
if (s1 == s2) // will be false
if (s1.equals(s2)) // will be true (content comparison)
Now, for the same code, if you replace Line 2 with statement
String s2 = new String("Hello").intern();
s1 == s2 // will be true as the second String object "Hello" is also intern'd.
</code>
HTH
 
Viji Bharat
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Prasad:
intern() method returns a String. So, if your code is
<code>
String s1 = new String("javaranch").intern();
</code>
you would get the expected results.
HTH
 
Prasad Ballari
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Viji
Thank you very much.I got the answer.I tried with this.The answer is perfect
public class StringTest{
public static void main(String[] args){
String s1=new String("javaranch").intern(); //putting in the pool
String s2=new String("javaranch").intern(); //getting from the pool
String s3="javaranch"; //getting from the pool
if (s1==s2){
System.out.println("s1==s2");
}
if (s1.equals(s2)){
System.out.println("s1 equals s2");
}
if (s1==s3){
System.out.println("s1==s3");
}
if (s1.equals(s3)){
System.out.println("s1 equals s3");
}
}

Regards
Prasad

}
[This message has been edited by Prasad Ballari (edited November 14, 2000).]
 
Greenhorn
Posts: 15
jQuery Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all!!

Maybe old

...but why use



instead just?



What is the utility of String.intern() method if Strings without "new" are pooled by default?

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
Howdy SCJPers!

This thread about the intern method might help in understanding the String pool, but I want to let everyone kow that the intern method is NOT on the exam!

hth,

Bert
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic