All for Joomla All for Webmasters

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:

  1. public String intern()

Returns

interned string


Java String intern() method example

  1. public class InternExample{
  2. public static void main(String args[]){
  3. String s1=new String(“hello”);
  4. String s2=“hello”;
  5. String s3=s1.intern();//returns string from pool, now it will be same as s2
  6. System.out.println(s1==s2);//false because reference is different
  7. System.out.println(s2==s3);//true because reference is same
  8. }}

Output:

false
true
PinIt
submit to reddit

Leave a Reply

Top