Java: Detailed Explanation of String Usage

String is a string class under the java.lang package, which is modified by final and cannot be inherited by other classes.

public final class String

The underlying implementation of String is an array of type char

private final char value[];

Because the array has a feature, once the array is created, the length cannot be changed, and the string cannot be modified after it is created.

How the string is stored in the memory:

Strings are reference data, which is different from general reference data. General reference data is stored in the heap memory, while the string is stored in the string constant pool in the static area .
(1) Create objects s1 and s2 by directly assigning literal values

String s1 = "abc";
String s2 = "abc";

In this case, the virtual machine first goes to the constant pool to see if there is a string with a value equal to abc. If there is no string at the beginning, a new string object will be created in the constant pool, and s1 will point to the object. When the second statement is executed , Because the object abc already exists in the constant pool, there is no need to create it again, just save the object address in s2. So the execution System.out.println(s1 == s2);will be correct, because the addresses saved in s1 and s2 are the same.

(2) Create a string object through the new keyword

	String s5 = new String("123");
	String s6 = new String("123");

Different from the first case, the two references of s5 and s6 no longer directly save the address of the object in the constant pool, but save the address of the heap memory. If there are several new keywords, the heap memory will open up several spaces, and then the heap memory will be saved. The object address of the constant pool.
When the first statement is executed, a space is created in the stack, the heap, and the constant pool. The constant pool saves data 123, the heap memory points to the address of the object in the constant pool, and the reference s5 in the stack saves the heap address. When the second sentence is executed, the heap memory opens up a second space, which also points to the object holding 123 in the constant, and s6 points to the heap. In this case, it System.out.println(s5 == s6);is false, because s5 and s6 store different heap addresses.

The way the String class creates objects (construction method)

1. The first type is to use literals, and the reference points to the constant pool object

	String s1 = "abc";

2. Using the public String(String original)construction method, the heap memory saves the object address in the constant pool, and s2 points to the heap address

	String s2 = new String("abc");

3. Byte array, using public String(byte bytes[])construction method`

	byte[] aaa = {
    
     97, 98, 99 };
	String s3 = new String(aaa);
	String s4 = new String(new byte[] {
    
     97, 98, 99 });

4. Byte array, intercept a part, call the public String(byte bytes[], int offset, int length)construction method

	byte[] aaa = {
    
     97, 98, 99 };
	String s5 = new String(aaa, 1, 2);

5. Character array, call `public String(char value[]) construction method

	char[] chars = {
    
    'a','b','c','d'};
	String s6 = new String(chars);

6. Intercept a part and callpublic String(char value[], int offset, int count)

	char[] chars = {
    
    'a','b','c','d'};
	String s7 = new String(chars,2,2);

7, empty visit

	String s8 = new String();

Concatenation of strings

//a和b都是字面量,需要在编译阶段说明临时空间,所以需要通过值确定类型
//在编译阶段,+号会被去掉
String s2 = "a" + "b";
//a和b是变量,编译阶段是不需要确定变量的值的
//在运行的时候,两个字符串变量相加,会自动创建一个StringBuffer对象,然后把两个变量拼接到一起,最终转换为String类型
//以return new String(value, 0, count); 所以s3指向堆内存
String a = "a";
String b = "b";
String s3 = a + b;

Guess you like

Origin blog.csdn.net/qq_41504815/article/details/112910016