String String object

The way a string is created

String is an immutable class in Java, so it cannot be modified once instantiated. Once an instance of an immutable class is created, the values of its member variables cannot be modified. This design has many benefits, such as caching HashCode, ease of use, and greater security.

String s ="JAVA"; String b = new String("JAVA");

The difference between the two creation methods is that an object may be created or none may be created during initialization. The latter creates at least one, and possibly two, objects in memory because of the new keyword.

String s = “JAVA”; At compile time, the JVM goes to the constant pool to see if “JAVA” exists. If not, it creates a space in the constant pool to store “JAVA”. If it exists, there is no need to create new space. Then create a space in stack memory named JAVA to store the address value of “ABC” in the constant pool.

String b = new String(“JAVA”); At compile time, the JVM searches the constant pool to see if “JAVA” exists. If it does not, it creates a space in the constant pool to store “JAVA”. At runtime, the String constructor creates a new space in the heap memory, and then stores a copy of “JAVA” in the String pool into the heap space, creating a space named B in the stack to store the address value of the new String object in the heap.

The benefits of the side

1. You can cache hash values

Because String hash values are often used, for example String is used as the key of a HashMap. The immutable nature makes the hash value immutable, so it only needs to be evaluated once.

2. String Pool requirements

If a String object has already been created, the reference is fetched from the String Pool. String Pool can only be used if String is immutable.

3. The security

String is often used as a parameter, and String immutability ensures that the parameter is immutable. For example, if String is mutable as a network connection parameter, then during the network connection, String is changed, and the party that changed String thinks it is connected to another host, which is not necessarily the case.

4. Thread safety

String immutability is inherently thread-safe and can be used safely across multiple threads.

String location

In the JVM, to reduce repeated creation of the same string, to achieve memory savings. A separate area of memory is created for string constants, called the string constant pool.

In versions prior to JDK 7, string constant pools were placed in persistent generations. In JDK 7, the string constant pool was first removed from the permanent generation and temporarily placed in heap memory, since the JDK was scheduled to replace the permanent generation with a meta-space in a later release.

In JDK 8, the permanent generation was removed completely and replaced with a meta-space, so the string constant pool was once again moved from heap memory into the permanent generation

String length limit

Strings have a length limit. At compile time, constants in the string constant pool are required to be no more than 65535, and during javAC execution, the maximum value is 65534. At runtime, the length cannot exceed the range of Int, otherwise an exception will be thrown.

Hollischuang. Gitee. IO/tobetopjava…

The concatenation of strings

String concatenation methods include +, concat, or stringutils. join, StringBuffer, and StringBuilder instead.

Hollischuang. Gitee. IO/tobetopjava…