preface
How does the “+” concatenate strings? (JDK1.7 or above)
Conclusion:
When you use a “+” connection string, you actually use a StringBuilder object that you create temporarily. For compile-time constants, the value of the string is computed directly after compilation, rather than creating a temporary StringBuilder object to complete string concatenation at run time. To concatenate strings in loops, use StringBuilder directly instead of “+” to improve performance. Note: When using the operator “+” to concatenate strings, if both operands are compile-time constants, the value of the string is evaluated at compile time and the StringBuilder object is not created at run time.
final String s = "abc"; String x = "abc" + "def"; // Both operands are constants, and the runtime does not create a StringBuilder object String y = s + "def"; // s and "def" are compile-time constants. No StringBuilder object is created. In fact, x and y point to the same object, i.e. "abcdef" String z = y + "ABC "; // if y is a variable, the StringBuilder object is created at run time
A String that cannot be modified
<1> Q: Once a String is created, it cannot be modified. Why?
Because the String class is final, it cannot be inherited.
All member variables are private, and there are no public methods that modify private member variables. None of the operations on a String modify the current object, but create a new object
<2> Q: Is there any benefit to making strings immutable?
The biggest advantage is to realize resource sharing, multi-threaded operation, with thread safety.
Three points:
1. The String class is final and cannot be modified once its objects are created.
2.String methods that appear to modify character sequences actually return newly created strings, not modify their own objects.
3.String objects cannot be changed, so they are thread-safe and can be freely shared.
An internal implementation of the String class
Inside the String class, a character array char[] is used to maintain character sequences.
private final char value[];
Q: What is the maximum length of a String?
The maximum length of String is also the maximum length of char[]. In theory, it is the maximum length of int, namely 2147483647. In fact, the maximum value available is generally less than the theoretical maximum value.
Apply for an array of characters of maximum length int:
char c = new char[Integer.MAX_VALUE]; / / an error
Error: a char occupies 2 bytes and integer. MAX_VALUE char is close to 4GB. The memory overflowed due to such a large contiguous memory space.
// Join Java development chat
By default, the maximum size of the heap is 256MB. Ideally, if we make the maximum size of the Java heap large enough, we can apply the maximum character length, i.e. Integer.max_value.
Java – Xmx1G com. Fan. Fragmentlearning. STR. StringDemoMain – set maximum Java heap is the Main method
The String constant pool
<1> What is a String constant pool?
The String constant pool is private inside the String class and can automatically add String literal constants to it.
At first, the constant pool is empty. When a String literal constant appears in the program, the equals method of the String class is used to check whether the String literal exists in the constant pool. If not, the literal constant is added to the constant pool and the object is returned. Otherwise, the object in the constant pool is returned.
<2> String literal constants and String constant expressions are added to the constant pool.
<3> What criteria does a String constant expression meet?
That is, a String expression whose value can be determined at compile time. The compiler’s rule is that what can be evaluated at compile time is not left to be evaluated at run time.
First: expressions are all concatenated by String literal constants
String s = "a" + "b" + "c";
Second: expressions are composed of any combination of String literal constants, primitive literal constants, String references to final modifiers, and primitive data types of final modifiers.
String s = "a" + 5; // String constant expression final String STR = "a"; String s = "bc" + str; Final int num = 5; String s = "a" + num; String s = STR + num; // String constant expressionCopy the code
<4> Intern method – Detain string
If you need to add a String to the constant pool, you can call the intern method, also known as the detention String.
The system automatically adds String literal constants and String values of String constant expressions to the constant pool by calling intern.
Summary of main points:
The String class maintains a special area called the constant pool. Because strings are immutable, there is no need to create two identical strings. You can share strings simply by adding them to the constant pool and pulling them out as needed.
String compile-time constants (String literal constants and String constant expressions) are automatically called intern. Equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals Otherwise, add the object to the constant pool and return it.
For strings created at run time (non-string compile-time constants), they are allocated to the heap. The system does not automatically call intern to hold the object, but we can still call intern to hold the object ourselves.
The last
I here organized a Java information document, Spring series of family bucket, Java systematic information :(including Java core knowledge points, interview topics and 21 years of the latest Internet real questions, e-books, etc.) friends who need to pay attention to the public number can be obtained.