1. Two constructors of a string
Private Final Char value[]; Private int Hash; Public String () { this. Value = "". Value;}
Public string (string original) { this. Value = original.value; this. hash = Original.hash;}
2. How many string objects are created in the new string? 1) Creation of two string objects
Public Static void Main (string[] args) { new String ("Hello");}
The memory model is as follows:
2) Create a String object
Public Static void Main (string[] args) { = "Hello"; New String ("Hello");}
The memory model is as follows:
3. Intern method of String
The string class privately maintains an initially empty string pool when the Intern method is called:
1. If the string pool already contains a string equal to this string object (as determined by the Equals method ), the string in the pool is returned.
2. Otherwise, add this string object to the pool and return a reference to this string object.
Public native String intern ();
A String object reference is placed in the pool of strings, not the string object itself.
Experiment:
Public Static void Main (string[] args) { = "hel"; + = "Lo" ; = "Ja"; + = "va" ; // true // false}
y = = Y.intern () False indicates that "Java" already exists in the string pool, and the string loaded in the string pool when the JVM starts also includes: "True", "false" ...
Memory model:
Intern ago
After intern
Java String Pool