Java Intern function

Viewed 524

I am a bit confused about the intern function. I have the following code:

public class InternTest{

    public static void main(String args[]){
            String s1 = "ANEK";
            String s2 =  new String("ANEK");
            String s3 = s2.intern();
            System.out.println(s3 == s1); // True

            String s11 = "ANEK".concat("SINGH");
            String s22 = s11.intern();
            System.out.println(s11 == s22); // True

            String s4 = "nat".concat("ive");
            String s5 = s4.intern();
            System.out.println(s4 == s5); // True

            String s33 = "ma".concat("in");
            String s44 = s33.intern();
            System.out.println(s33 == s44); // false

            String s331 = "ja".concat("va");
            String s441 = s331.intern();
            System.out.println(s331 == s441); // false
    }
}

My question is regarding the output. In the third case it gives me true, but in the fourth and fifth case it gives me false. Can I known what is reason behind those output? I could not come to the conclusion that it gives false for java reserved word or key word because when I tried with en um it gives true but with by te it gives me false. Can any one tell me why?

3 Answers
Related