Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When are Java Strings interned?

Tags:

java

string

Inspired by the comments on this question, I'm pretty sure that Java Strings are interned at runtime rather than compile time - surely just the fact that classes can be compiled at different times, but would still point to the same reference at runtime.

I can't seem to find any evidence to back this up. Can anyone justify this?

like image 946
Noel M Avatar asked Aug 10 '10 16:08

Noel M


People also ask

Are strings interned in Java?

The Java String class intern() method returns the interned string. It returns the canonical representation of string. It can be used to return string from memory if it is created by a new keyword. It creates an exact copy of the heap string object in the String Constant Pool.

What is an interned String object used for in Java?

In computer science, string interning is a method of storing only one copy of each distinct string value, which must be immutable. Interning strings makes some string processing tasks more time- or space-efficient at the cost of requiring more time when the string is created or interned.

Does Java intern strings by default?

Though Java automatically interns all Strings by default, remember that we only need to intern strings when they are not constants, and we want to be able to quickly compare them to other interned strings.

What is intern () in string?

The method intern() creates an exact copy of a String object in the heap memory and stores it in the String constant pool. Note that, if another String with the same contents exists in the String constant pool, then a new object won't be created and the new reference will point to the other String.


1 Answers

The optimization happens (or at least can happen) in both places:

  • If two references to the same string constant appear in the same class, I'd expect the class file to only contain one constant pool entry. This isn't strictly required in order to ensure that there's only one String object created in the JVM, but it's an obvious optimization to make. This isn't actually interning as such - just constant optimization.
  • When classes are loaded, the string pool for the class is added to the intern pool. This is "real" interning.

(I have a vague recollection that one of the bits of work for Java 7 around "small jar files" included a single string pool for the whole jar file... but I could be very wrong.)

EDIT: Section 5.1 of the JVM spec, "The Runtime Constant Pool" goes into details of this:

To derive a string literal, the Java virtual machine examines the sequence of characters given by the CONSTANT_String_info structure.

  • If the method String.intern has previously been called on an instance of class String containing a sequence of Unicode characters identical to that given by the CONSTANT_String_info structure, then the result of string literal derivation is a reference to that same instance of class String.

  • Otherwise, a new instance of class String is created containing the sequence of Unicode characters given by the CONSTANT_String_info structure; that class instance is the result of string literal derivation. Finally, the intern method of the new String instance is invoked.

like image 170
Jon Skeet Avatar answered Sep 20 '22 19:09

Jon Skeet