4

Possible Duplicate:
When should we use intern method of String?
what is string interning?

Please explain the inner workings of the following code:

System.out.println(new String("ABC").intern()==new String("ABC").intern());

In the above code it prints "true". But according java rules, in the case of the new operator, it always creates a new object. object.intern() method also creates an object in the string pool. So my question is, in the above code how many objects are created.

According to me, 3 new objects will created. One goes to String pool, and two anonymous objects will be created by the new operator. But i am not sure.

If i am wrong please explain.

Community
  • 1
  • 1
Madhusudan
  • 51
  • 2

3 Answers3

1

Assuming no cleverness in the optimizer, two objects are created. (A smart enough optimizer could optimize this to just an unconditional true, in which case no objects are created.)

tl;dr version: You were almost right with your answer of 3, except that the string that goes into the String pool is not generated as part of this statement; it's already created.

First, let's get the "ABC" literal out of the way. It's represented in the runtime as a String object, but that lives in pergen and was created once in the whole life of the JVM. If this is the first class that uses that string literal, it was created at class load time (see JLS 12.5, which states that the String was created when the class was loaded, unless it previously existed).

So, the first new String("ABC") creates one String, which simply copies the reference (but does not create a new object) to the chars array and hash from the String that represents the "ABC" literal (which, again, is not created as part of this line). The .intern() method then looks to see whether an equal String is already in permgen. It is (it's just the String that represents the literal to begin with), so that's what that function returns. So, new String("ABC").intern() == "ABC". See JLS 3.10.5, and in particular:

Moreover, a string literal always refers to the same instance of class String. This is because string literals - or, more generally, strings that are the values of constant expressions (§15.28) - are "interned" so as to share unique instances, using the method String.intern.

The same thing exactly happens with the second occurrence of new String("ABC").intern(). And, since both intern() method return the same object as the "ABC" literal, they represent the same value.

Breaking it down a bit:

String a = new String("ABC"); // a != "ABC"
String aInterned = a.intern(); // aInterned == "ABC"

String b = new String("ABC"); // b != "ABC"
String bInterned = b.intern(); // bInterned == "ABC"

System.out.println(new String("ABC").intern()==new String("ABC").intern());
                                            // ... is equivalent to... 
System.out.println(aInterned == bInterned); // ...which is equivalent to...
System.out.println("ABC" == "ABC");         // ...which is always true.
yshavit
  • 42,327
  • 7
  • 87
  • 124
0

When you call intern() method, jvm will check if the given string is there, in string pool or not. If it is there, it will return a reference to that, otherwise it will create a new string in pool and return reference to that. In your case : System.out.println(new String("ABC").intern()==new String("ABC").intern());

The first new String("ABC").intern() will create a string "ABC" in pool.When you call new String("ABC").intern() second time, jvm will return the reference to previously created string.That is the reason you are getting true when comparing both(btn are pointing to same reference).

Theocharis K.
  • 1,281
  • 1
  • 16
  • 43
Renjith
  • 3,274
  • 19
  • 39
0

I believe you are right, as new operation create a new object so there are 2 anonymous objects and intern() creates a new string in the string pool only if it is not already and returns it's reference

vishal_aim
  • 7,636
  • 1
  • 20
  • 23