Assuming there is no trick in the optimizer, two objects are created. (A simplified enough optimizer can optimize this to an unconditional true , in which case no objects are created.)
tl; dr version:. You were almost right with your answer 3, except that the row that is part of the row pool is not generated as part of this statement; it is already created.
First, let me get the letter "ABC" . It is presented at run time as a String object, but it lives in a pergene and was created once in the life of the JVM. If this is the first class that uses this string literal, it was created at the time the class was loaded (see JLS 12.5 , which states that String was created when the class was loaded, unless it existed previously).
So, the first new String("ABC") creates a single String that simply copies the link (but does not create a new object) to an array of characters and a hash from the string that represents the literal "ABC" (which, again, is not created as part this line). Then the .intern() method checks to see if the equal String is already in permgen. This (this is just a string that represents a literal to start with) to return this function. So new String("ABC").intern() == "ABC" . See JLS 3.10.5 and in particular:
In addition, a string literal always refers to the same instance of the String class. This is because string literals, or, more generally, strings that are constant expression values ββ(Β§15.28), are βinternedβ to exchange unique instances using the String.intern method.
The same thing happens with the second occurrence of new String("ABC").intern() . And since both intern() methods return the same object as the "ABC" literal, they represent the same value.
A little break:
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.
source share