5.4 String Resolution
A constant pool entry tagged as CONSTANT_String (�4.4.3) represents an instance of a string literal (�2.3), that is, a literal of the built-in type java.lang.String.
The Unicode characters (�2.1) of the string literal represented by the CONSTANT_String entry are found in the CONSTANT_Utf8 (�4.4.7) constant pool entry that
the CONSTANT_String entry references.
The Java language requires that identical string literals (that is, literals that contain the same sequence of Unicode characters) must reference the same instance of class
String. In addition, if the method intern is called on any string, the result is a reference to the same class instance that would be returned if that string appeared as
a literal. Thus,
("a" + "b" + "c").intern() == "abc"
must have the value true.3
To resolve a constant pool entry tagged CONSTANT_String, the Java Virtual Machine examines the series of Unicode characters represented by the UTF-8 string
that the CONSTANT_String entry references.
If another constant pool entry tagged CONSTANT_String and representing the identical sequence of Unicode characters has already been resolved, then the
result of resolution is a reference to the instance of class String created for that earlier constant pool entry.
Otherwise, if the method intern has previously been called on an instance of class String containing a sequence of Unicode characters identical to that
represented by the constant pool entry, then the result of resolution 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 represented by the CONSTANT_String entry; that class
instance is the result of resolution.
This example illustrates six points:
Literal strings within the same class (�8) in the same package (�7) represent references to the same String object (�4.3.1).
Literal strings within different classes in the same package represent references to the same String object.
Literal strings within different classes in different packages likewise represent references to the same String object.
Strings computed by constant expressions (�15.27) are computed at compile time and then treated as if they were literals.
Strings computed at run time are newly created and therefore distinct.
The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents.
"JavaRanch, where the deer and the Certified play" - David O'Meara
I like you because you always keep good, crunchy cereal in your pantry. This tiny ad agrees:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|