Java String intern
The java string intern() method returns the interned string. It returns the canonical representation of string.
It can be used to return string from pool memory, if it is created by new keyword.
Signature
The signature of intern method is given below:
public String intern()
Returns
interned string
Java String intern() method example
public class InternExample{
public static void main(String args[]){
String s1=new String("hello");
String s2="hello";
String s3=s1.intern();//returns string from pool, now it will be same as s2
System.out.println(s1==s2);//false because reference is different
System.out.println(s2==s3);//true because reference is same
}}
Expected Output:
falsetrue
No comments:
Post a Comment