How does the “+” concatenate strings? (JDK1.7 or above)Conclusion:

When you use a “+” connection string, you’re actually using a StringBuilder object that you created 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. When concatenating strings in loops, use StringBuilder 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 StringBuilder object is not created at runtime
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
Copy the code

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:

  • The String class is final and cannot be modified once its object is created.
  • String methods that appear to modify character sequences actually return newly created strings rather than modify their own objects.
  • Strings are immutable and therefore thread-safe and freely shareable.

Inside the String class, a character array char[] is used to maintain character sequences.

private final char value[];
Copy the code

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
Copy the code

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";
Copy the code

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; // String constant expression

final int num = 5;
String s = "a" + num; // String constant expression

String s = str + num; // String constant expression
Copy 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.

Mysql, Netty, Spring, thread, Spring Cloud, JVM, source code, algorithm, etc., also have a detailed learning plan map, interview questions, etc., need to obtain these contents of the friend please add Q: sample: 756584822