Using intern method of String class in java



Contents of page :
  • About intern method of String class in java
  • Example of intern method of String in java -
  • Program to demonstrate intern method of String in java>
  • About Interned string from java docs>

First I’ll recommend you to read String pool java.


1) About intern method of String class in java
Method is found in java.lang.String class
When the intern method is invoked, if the string 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 string pool and a reference to this String object is returned.

2) Example of intern method of String in java -

String s1 = "abc";    
"abc" is created in string pool i.e. String object is added to the string pool.



String s5 = new String("abc").intern();
What happens internally When above statement is executed ?   
When the intern method is invoked, if the string pool already contains a string equal to this String object will be determined by the equals(Object) method.
As “abc” already exists in string pool (Because of  String s1 = "abc").
So, "abc".equals("abc") will return true and s5 will be a reference variable which will refer to "abc" in string pool.

So, s1 == s5 will always return true.


3) Program to demonstrate intern method of String in java>
In the below program reference equality have been done using == operator.
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class StringPoolTest {
   public static void main(String[] args) {
         
          String s1 = "abc";
          String s2 = new String("abc");
          String s3 = "abc";
          String s4 = new String("abc");
          String s5 = new String("abc").intern();
          System.out.println("--comparing s1--");
          System.out.println(s1==s2); //false
          System.out.println(s1==s3); //true
          System.out.println(s1==s4); //false
          System.out.println(s1==s5); //true
          System.out.println("--comparing s2--");
          System.out.println(s2==s3); //false
          System.out.println(s2==s4); //false
          System.out.println(s2==s5); //false
         
          System.out.println("--comparing s3--");
          System.out.println(s3==s4); //false
          System.out.println(s3==s5); //true
         
          System.out.println("--comparing s4--");
          System.out.println(s4==s5); //false
         
   }
}
/* OUTPUT
--comparing s1--
false
true
false
true
--comparing s2--
false
false
false
--comparing s3--
false
true
--comparing s4--
false
*/



4) About Interned string from java docs>
In JDK 7, interned strings are no longer allocated in the permanent generation of the Java heap, but are instead allocated in the main part of the Java heap (known as the young and old generations), along with the other objects created by the application. This change will result in more data residing in the main Java heap, and less data in the permanent generation, and thus may require heap sizes to be adjusted. Most applications will see only relatively small differences in heap usage due to this change, but larger applications that load many classes or make heavy use of the String.intern() method will see more significant differences.


Summary -
So in this core java tutorial we learned About intern method of String class in java. Example of intern method of String in java. Program to demonstrate intern method of String in java. About Interned string from java docs.




Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.


RELATED LINKS>

Difference between equals method and == operator in java - testing with String and StringBuffer

Difference between String, StringBuffer and StringBuilder in java - In depth coverage

String is Immutable in java




Labels: Core Java
eEdit
Must read for you :