Detailed explanation of String in Java

Detailed String in Java
String: String Constant Pool

Interview questions (look at the video of station b)

1. Can I not use volatile with String?

2. Why are there these two ways? Because of their different storage areas

3. The bottom layer of String is an array of type char

1. Three characteristics of String

1.1. Immutability

1.1.1. String is an object in immutable mode. The main function of invariant mode is to ensure data consistency when an object needs to be shared by multiple threads and accessed frequently.

1.2, constant pool optimization

1.2.1. After the String object is created, it will be cached in the string constant pool, and the cached reference will be returned directly when the same object is created next time.

1.3、final

1.3.1. The String class cannot be inherited, which improves the security of the system.

2. There are three ways to instantiate String ():

2.1, direct assignment

2.2. Through the constructor, the value of the string can be passed in directly, or a char array can be passed in.

2.3. Through the constructor, you can directly pass in an array of type char

The difference between the two (storage area): direct assignment is stored in the string constant pool, and by constructing an instance, it is stored in the heap.

3. Summary of interview test sites

3.1, String is not a basic data type

The bottom layer of String is actually an array of type char

3.2, the instantiation of String

3.3, equals method

public boolean equals(Object anObject) {
    
        
if (this == anObject) {
    
           
    return true;   
 }   
 if (anObject instanceof String) {
    
          
   String aString = (String)anObject;      
     if (coder() == aString.coder()) {
    
             
        return isLatin1() ? StringLatin1.equals(value, aString.value) : 			           					  
                            StringUTF16.equals(value, aString.value);       
    }    }   
     return false; 
     }

3.3.1. The equals method in Object is the same as "==".

3.3.2. String rewrites the equal method in Object. The comparison is no longer the address value, but its value ().

3.4, String is immutable

3.4.1. As long as the value of String is changed, it becomes a new object

3.5, intern() method

3.5.1. When the String object calls the intern() method, it will go to the string constant pool to find the value; if the value exists, a reference to the value is returned; if it does not exist, a new one is created in the string constant pool Address value and return.

Four, commonly used methods

Insert picture description here

4.1, string cutting

4.1.1, split() method supports regular expressions

Five, interview classic questions

5.1 What is the difference between == and equals?

5.1.1. "==" is compared from the data type classification, if the basic data type is compared, it is the comparison value; if the reference data type is compared, it is the address value in the memory.

5.1.2, equals is the method provided by Object and "==" is the same, but String rewrites it, and compares its value, not the address value in memory.

5.2. The result of running the following code is:

String str1 = "Hello World"; 
String str2 = "Hello"; 
str2 += " World"; 
System.out.println(str1 == str2);

You can use the command javap -c xx.class to see the execution order. In the process, there is a new StringBuilder(); method append, so str2 appears in the heap, and the result is false;

Insert picture description here

5.3. The result of running the following code is:

        String str1 = "Hello World";
        final String str2 = " World";
        String str3 = "Hello" + str2;
        System.out.println(str1 == str3);

If the result is true, the final modification will be directly compiled into an explicit value, and the final modification will not be compiled into an explicit value.
Reference: The difference between final modified String variables and unmodified String variables in java

5.4. The result of running the following code is:

String str1 = "Hello World"; 
final String str2 = new String(" World"); 
String str3 = "Hello"+str2; 
System.out.println(str1 == str3);

If the result is false, it is modified by final, but it was created in heap memory at the beginning, so no matter whether it is modified by final or not, it is always in heap memory.

Insert picture description here

5.5. The result of running the following code is:

        String str1 = "Hello World";
        String str2 = "Hello";
        String str3 = " World";
        String str4 = str2 + str3;
        System.out.println(str4 == str1);
        System.out.println(str4.intern() == str1);

The result is: false, true. Because String's intern(); method searches for the string constant pool first, returns the reference if there is one, and creates it in the constant pool if there is no.

5.6, the role of string constant pool

Multiple identical values ​​can share one address value. Can reduce memory consumption.

5.7. Is String thread safe?

Yes, the value is immutable, as long as the value changes, it is a new address value.

5.8. What are the benefits of using String as key when using HashMap?

Since HashMap finds the value according to the hashcode of the key, the String is cached when it is created. So in HashMap, there is no need to calculate the hashcode, it is in the cache, and the efficiency is higher than other objects.

Guess you like

Origin blog.csdn.net/weixin_43983411/article/details/108569701