intern() in java

 intern()


public class Javaoneworld {
    public static void main(String args[]) {
      
      String str1 = new String ("javaoneworld"); // 
      String str2  = new String ("javaoneworld");

/*
	 * as we initializing str1 and str2 with new keyword so they will not share the
	 * same memory pool
	 */
      System.out.println(str1 == str2); //false
      
      /*
	 * using the intern() method we can force both the string to share same memory
	 * pool
	 */
      
      str1 = str1.intern();
      str2 = str2.intern();
      
      System.out.println(str1 == str2); //true
    }
}
   







No comments:

Post a Comment