tags:

views:

83

answers:

1

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?

+5  A: 

It's both:

  • If two references to the same string constant appear in the same class, the class file will only contain one string pool entry
  • When classes are loaded, the string pool for the class is added to the intern pool

(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.

Jon Skeet
I remember somewhere I read said that the string pool are scoped by packages. I cannot recall the source of it
Dennis Cheung
@Dennis: That sounds inaccurate to me. Section 3.10.5 of the JLS doesn't mention that as far as I can see.
Jon Skeet
(+1) I followed the link in the [comments that lead to this question](http://stackoverflow.com/questions/3450604/why-is-there-no-string-empty-in-java/3450623#3450623) to answer with references to the same documentation, only to find that Jon Skeet was already on the case. :P
Tim Stone
Since 1.5 there has been the pack200 distribution format. The dedupes much information contained in jar files. However, when used it recreates the jar file (which is a bit daft, but there you go).
Tom Hawtin - tackline