Tuesday 17 June 2014

intern()

public String intern()
Returns a canonical representation for the string object. All literal strings and string-valued constant expressions are interned.

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. 

class StringIntern{
 public static void main(String args[]){
  String s1 = "wxyz";
  String s2 = new String("wxyz"); 
  String s3 = new String("wxyz").intern(); 
  
  System.out.println(s1 == s2); // will return false
  System.out.println(s1 == s3); // will return true
  System.out.println(s2 == s3); // will return false
 }
}

Output
false
true
false


 
Prevoius                                                 Next                                                 Home

No comments:

Post a Comment