Java basic knowledge summary of String

Java basic knowledge summary of String

Get to know String:

​ is located in the java.lang package; conceptually, a Java string is a sequence of characters in a Unicode string.

String key points:

​ The focus of String's understanding is the understanding that a string is a constant; a string is stored in a constant pool. This is also the key point of common interviews about strings in java.

Several common interview questions about String in Java:

​ The first thing to be clear is that "==" in Java is the reference address value for comparison, and the equals() method is to compare whether the value is equal (in terms of String comparison);

no.1

String s1 = "abc";
String s2 = "abc";
System.out.println(s1 == s2);// true
System.out.println(s1.equals(s2));// true

​ At this time, s1 and s2 both point to "abc" in the constant pool. You can draw the memory allocation diagram of this code by yourself.

Insert picture description here

no.2

String s1 = new String("abc");
String s2 = "abc";
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));

​ Here s1 uses the keyword new; we all know that using the keyword new will open up a space in the heap, so the variable s1 points to the memory space in the heap, and of course s2 points to the constant pool. String. The memory allocation diagram is roughly as follows:

Insert picture description here

​ At this time, when s1 and s2 are compared with "==", it is to compare whether the reference values ​​of the respective variables are the same. According to the above figure, they must be different.

no.3

String s1 = "a"+"b"+"c";
String s2 = "abc";
System.out.println(s1 == s2);// true
System.out.println(s1.equals(s2));// true

​ Here s1 is optimized to s1 = "abc" during the compilation process; so when s1 == s2 is executed, the output is true;

String a = "a";
String b = "b";
String c = "c";

String s1 = a+b+c;
String s2 = "abc";
System.out.println(s1 == s2);// false
System.out.println(s1.equals(s2));// true

​ Here s1 uses a string variable for the "+" operation, which will be optimized to the append() method operation in StringBuilid during compilation, and finally the StringBuilder.toString operation; you can see from the source code of StringBuilder that this is the execution of new String ()operating. and so

@Override
public String toString() {
    
    
    // Create a copy, don't share the array
    return new String(value, 0, count);
}

no.4

String s1 = new String("abc");
String si = s1.intern();
String s2 = "abc";
System.out.println(s1 == s2);// false
System.out.println(si == s2);// true

Here is mainly an understanding of the intern() method; the intern() method is a native method (a method directly written in c language or c++ language). The main function of the intern() method is to check whether there is this string in the string constant pool, if it exists, return this string, if it does not exist, save a copy, and then return.

public native String intern();

Memory allocation diagram:

Insert picture description here

no.5

String s1 = new String("zh")+new String("y");
String si = s1.intern();
String s2 = "zhy";
System.out.println(s1 == s2);// true
System.out.println(si == s2);// true

This result is modified according to jdk1.7. In the previous version, the intern() method was executed to determine whether the constant value of a string is contained in the constant pool. After 1.7, it is determined to copy the address value to the constant pool. So it resulted in the result in no.5.

Insert picture description here

The judgment of the results of no4 and no.5 depends on what value is put into the constant pool when intern is executed, and then what value is taken from the constant pool.

Reference materials:
https://blog.csdn.net/Sqirt/article/details/72765071
https://blog.csdn.net/superlover_/article/details/93719290

BGM :

Leslie Cheung "Monica" (the song I listened to when I wrote this article:))

Guess you like

Origin blog.csdn.net/Bruce_Zhang0828/article/details/94362666