0

The Java Language Specification states that

When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(java.lang.Object) method, then the string from the pool is returned

In the following code snippet:

class StringPoolTest {
  public static void main(String[] args) { 

    String first = "string";
    String second = new String("string");
    String third = "string".intern();

    System.out.println(System.identityHashCode(first));
    System.out.println(System.identityHashCode(second));
    System.out.println(System.identityHashCode(third));
  }
}

Output:

989184670
268130470
989184670

I added a String object first to the pool (assigning a String literal to the reference) and explicitly created second using a String constructor. There are now two identical character sequences in the pool.

I wanted to see which one would be returned when calling intern. Since the method hashCode is overridden for the class String, I used System.identityHashCode to see exactly which two String references were the same.

Clearly, intern returned the reference to the object created using a String literal. Why is this so? Are there any rules regarding which reference is returned in the case of multiple identical String objects in the pool?

0lt
  • 283
  • 2
  • 13
  • 2
    "There are now two identical character sequences in the pool." No, `new String("string")` creates a new string, equal to `"string"`, which is not in the pool. – Andy Turner Jan 09 '18 at 12:32
  • @AndyTurner not all String objects are in the pool? – 0lt Jan 09 '18 at 12:33
  • 2
    If all string objects were in the pool, what would be the need for the `intern()` method? – Andy Turner Jan 09 '18 at 12:33
  • @0lt: Correct, not all String objects are in the pool. Only *interned* String objects are in the string pool. All literals are interned when the class containing them is loaded. Others are only interned on request. – T.J. Crowder Jan 09 '18 at 12:34
  • Well, this is a revelation. Thanks! – 0lt Jan 09 '18 at 12:36
  • 2
    Possible duplicate of [*How does String.intern() work and how does it affect the String pool?*](https://stackoverflow.com/questions/30932486/how-does-string-interns-work-and-how-does-it-affect-the-string-pool) (And a related duplicate of it from yesterday which may also help as it's quite similar to the above: https://stackoverflow.com/questions/48139521/string-pool-behaves-differently-when-used-with-concat).) – T.J. Crowder Jan 09 '18 at 12:36
  • There are no "multiple identical String objects in the pool". They are all unique. – Klitos Kyriacou Jan 09 '18 at 13:30
  • Possible duplicate of [How does String.intern() work and how does it affect the String pool?](https://stackoverflow.com/questions/30932486/how-does-string-intern-work-and-how-does-it-affect-the-string-pool) – 0lt Jan 09 '18 at 13:45

1 Answers1

0

When a string literal(without using new Operator) is created, the JVM checks whether that String exists in the internal list or not. If it already exists in the list, then it does not create a new String and it uses reference to the existing String Object.

JVM does this type of checking internally for String literal but not for String object, which it creates through 'new' keyword i.e. using new always a new string is returned.

Har Krishan
  • 273
  • 1
  • 11