Sponsored Link •
|
Advertisement
|
CONSTANT_String_info
EntriesTo resolve an entry of type CONSTANT_String_info
, the virtual machine must
place a reference to an interned String
object in the data for the
constant pool entry being resolved. The String
object (an instance of class
java.lang.String
) must have the character sequence specified by the
CONSTANT_Utf8_info
entry identified by the string_index
item of the CONSTANT_String_info
.
Each Java virtual machine must maintain an internal list of references to String
objects that have been "interned" during the course of running the application. Basically, a
String
object is said to be interned simply if it appears in the virtual machine's internal
list of interned String
objects. The point of maintaining this list is that any particular
sequence of characters is guaranteed to appear in the list no more than once.
To intern a sequence of characters represented by a CONSTANT_String_info
entry, the virtual machine checks to see if the sequence of characters is already in the list of interned strings.
If so, the virtual machine uses the reference to the existing, previously-interned String
object. Otherwise, the virtual machine creates a new String
object with the proper
character sequence and adds a reference to that String
object to the list. To complete
the resolution process for a CONSTANT_String_info
entry, the virtual machine
places the reference to the interned String
object in the data of the constant pool
entry being resolved.
In your Java programs, you can intern a string by invoking the intern()
method
of class String
. All literal strings are interned via the process of resolving
CONSTANT_String_info
entries. If a string with the same sequence of Unicode
characters has been previously interned, the intern()
method returns a reference to
the matching already-interned String
object. If the intern()
method is invoked on a String
object that contains a sequence of characters that has
not yet been interned, that object itself will be interned. The intern()
method will
return a reference to the same String
object upon which it was invoked .
Here's an example:
// On CD-ROM in file linking/ex1/Example1.java class Example1 { // Assume this application is invoked with one command-line // argument, the string "Hi!". public static void main(String[] args) { // argZero, because it is assigned a String from the command // line, does not reference a string literal. This string // is not interned. String argZero = args[0]; // literalString, however, does reference a string literal. // It will be assigned a reference to a String with the value // "Hi!" by an instruction that references a // CONSTANT_String_info entry in the constant pool. The // "Hi!" string will be interned by this process. String literalString = "Hi!"; // At this point, there are two String objects on the heap // that have the value "Hi!". The one from arg[0], which // isn't interned, and the one from the literal, which // is interned. System.out.print("Before interning argZero: "); if (argZero == literalString) { System.out.println("they're the same string object!"); } else { System.out.println("they're different string objects."); } // argZero.intern() returns the reference to the literal // string "Hi!" that is already interned. Now both argZero // and literalString have the same value. The non-interned // version of "Hi!" is now available for garbage collection. argZero = argZero.intern(); System.out.print("After interning argZero: "); if (argZero == literalString) { System.out.println("they're the same string object!"); } else { System.out.println("they're different string objects."); } } }When executed with the string
"Hi!"
as the first command-line argument, the
Example1
application prints the following:
Before interning argZero: they're different string objects. After interning argZero: they're the same string object!
The CONSTANT_Integer_info
,
CONSTANT_Long_info
, CONSTANT_Float_info
,
CONSTANT_Double_info
entries contain the constant values they represent within
the entry itself. These are straightforward to resolve. To resolve this kind of entry, many virtual machine
implementations may not have to do anything but use the value as is. Other implementations, however, may
choose to do some processing on it. For example, a virtual machine on a little-endian machine could choose
to swap the byte order of the value at resolve time.
Entries of type CONSTANT_Utf8_info
and
CONSTANT_NameAndType_info
are never referred to directly by instructions. They
are only referred to via other types of entries, and resolved when those referring entries are resolved.
Sponsored Links
|