Interview bytes I was asked to death by String class questions!

insert image description here
How does "+" concatenate strings? (JDK1.7 and above)

Summarize:

When using "+" to concatenate strings, this is actually done using a temporarily created StringBuilder object.
For compile-time constants, the value of the string is calculated directly after compilation, without creating a temporary StringBuilder object at runtime to complete string concatenation.
To concatenate String objects in a loop, you should use StringBuilder directly instead of "+", which can improve performance.
Note: When using the operator '+' to concatenate strings, if both operands are compile-time constants, the value of the string is computed at compile time without creating a StringBuilder object at run time.


final String s = "abc";
String x = "abc" + "def"; // 两个操作数都是常量,运行时不会创建StringBuilder对象
String y = s + "def"; // s和"def"都是编译时常量,不会创建StringBuilder对象,实际上x和y指向同一个对象,即"abcdef"
String z = y + "abc"; // y是变量,则在运行时会创建StringBuilder对象

insert image description here

unmodifiable String object

<1> Q: Once a String object is created, it cannot be modified. Why?

Because the String class is of final type, it cannot inherit from this class.

All its member variables are private, and there is no public (public) method for modifying private member variables.
Operations on String objects do not modify the current object, but create a new object.

<2> Q: Is there any benefit in designing String objects to be unmodifiable?

The biggest advantage is that it can realize resource sharing, and it has thread safety in multi-threaded operation.

Three points to sum up:

  • The String class is final, and once its object is created, it cannot be modified.
  • The methods of the String class that seem to modify the sequence of characters actually return the newly created String object, rather than modifying the object itself.
  • String objects are immutable and therefore thread-safe and can be freely shared.

Internal implementation
of the String class Inside the String class, a character array char[] is used to maintain the character sequence.

private final char value[];

Q: What is the maximum length of a String?

The maximum length of String is also the maximum length of the character array char[], which is theoretically the maximum value of the int type, that is, 2147483647. In fact, the generally available maximum value is less than the theoretical maximum value.

Allocate an int max-length character array:

char c = new char[Integer.MAX_VALUE]; // 报错

Error: A char type occupies 2 bytes, and Integer.MAX_VALUE char types are close to 4GB in size. Applying for such a large continuous memory space will cause memory overflow.
//Join the Java development and exchange Jun-like: 756584822 blow water and chat together

Analysis: Objects in Java are allocated on the heap. The default maximum heap space is 256MB. In an ideal situation, as long as we set the maximum Java heap large enough, we can apply for the maximum character length, that is Integer.MAX_VALUE.

java -Xmx1G com.fan.fragmentlearning.str.StringDemoMain - set the Main method maximum Java heap

String constant pool

<1> What is String constant pool?

The String constant pool is private within the String class, and String literal constants can be automatically added to it.
At first, the constant pool is empty. When a String literal constant appears in the program, it will search for the existence of the String object in the constant pool, that is, use the equals method of the String class to judge. If it does not exist, add the literal constant to the constant. pool, and return the object; otherwise, return the object in the constant pool directly.

<2> Both String literal constants and String constant expressions are added to the constant pool.

<3> What conditions are met to be considered as String constant expressions?

That is, an expression of type String whose value can be determined at compile time. The principle of the compiler is that what can be calculated at compile time will not be calculated at runtime.

The first: expressions are all concatenated by String literal constants

String s = "a" + "b" + "c";

The second: The expression consists of String literal constants, basic type literal constants, final modified String references, and final modified basic data types in any combination.

String s = "a" + 5; // String常量表达式

final String str = "a";
String s = "bc" + str; // String常量表达式

final int num = 5;
String s = "a" + num; // String常量表达式

String s = str + num; // String常量表达式

<4> intern method - intern string

If you need to add a String object to the constant pool, you can call the intern method to complete, this operation is also called detention string.
The system automatically adds String literal constants and string values ​​of String constant expressions to the constant pool, which is also achieved by calling the intern method.

Summary of key points:

The String class maintains a special area called the constant pool. Because String objects are immutable, there is no need to create two identical String objects. Just add the String object to the constant pool and take it out when needed, so that the sharing of the String object can be realized.
When String compile-time constants (String literal constants and String constant expressions) appear in the program, the intern method will be called automatically. If the constant pool contains equal String objects (using the equals method of the String class to judge), the constant pool will be returned directly. The object in; otherwise, the object is added to the constant pool and returned.
For String objects (non-String compile-time constants) created at runtime, they will be allocated to the heap, and the system will not automatically call the intern method to detain the object, but we can still call the intern method of the object to detain the object by ourselves.

Finally, I wish everyone an early success in learning and a satisfactory offer

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324085587&siteId=291194637