Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Intern function [duplicate]

Tags:

java

string

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?

like image 650
Aravind E Avatar asked Aug 09 '17 06:08

Aravind E


2 Answers

Well you get the output that you do because the java and main Strings are already in the pool - when you start the application there are a lot of other classes that are loaded, and some of them have already interned those Strings (before your code is reached)

I would expect native also - but I guess not, meaning no one else interns it.

This has seen some attention (that I did not expect), so I thought I would expand this a little bit. Suppose you do this:

  String left = new String(new char[] { 'h', 'e', 'y' });
  String right = left.intern();
  System.out.println(left == right);

This will print true because "hey" was not in the String pool before and was added by our left.intern(). On the other hand:

 String left = new String(new char[] { 'j', 'a', 'v', 'a' });
 String right = left.intern();
 System.out.println(left == right);

prints false because "java" was already in the pool when we called left.intern.

like image 71
Eugene Avatar answered Nov 15 '22 08:11

Eugene


Lets see the documentation of intern():

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.

This means that if there is already a string with the given value in the pool that old string is returned, otherwise the actual string is returned. Since first three strings are not in the pool, the actual strings - s2, s11 and s4 are returned, after being added to the pool, so it's the same reference. Somehow '"main"' and '"java"' are already in the pool and intern() is returning that string instead of s33 or s331 (assuming s331.intern() for last call).

Try this:

String tmp = "ANEKSINGH";  // so this is in the pool
String s11 = "ANEK".concat("SINGH");
String s22 = s11.intern();
System.out.println(s11 == s22); // FALSE now
like image 7
user85421 Avatar answered Nov 15 '22 08:11

user85421