Saturday, 18 June 2016

Program to prove that String Object is immutable or an example of String Pooling.

Code:

public class StringPooling
{
    public static void main( String args[])
    {
        String s1="a";
        String s2="a";
        System.out.println(s1==s2);//true
        System.out.println(s1.equals(s2));//true
       
        String s3=new String("a");
        String s4=new String("a");
        System.out.println(s3==s4);//false
        System.out.println(s3.equals(s4));//true
       
        String s5="a";
        String s6=new String("a");
        System.out.println(s5==s6);//false
        System.out.println(s5.equals(s6));//true
    }
}

No comments:

Post a Comment