string interview questions, after reading these five you are guaranteed to gain something

 [Recommended] The latest collection of Java e-books in 2020.pdf (Hematemesis) >>>

 https://www.cnblogs.com/xiaogeng88/p/12692306.html

 

In this article, let ’s take a look at the 5 interview questions about the Java String class. Of these five questions, I personally experienced a few questions during the interview. After learning this, I realized it and realized why this answer is. This article takes you to understand why the answers to these questions are so.

1. Determine whether st1 and st2 defined as String type are equal, why

package string;

public class Demo2_String {

  public static void main(String[] args) {
    String st1 = "abc";
    String st2 = "abc";
    System.out.println(st1 == st2);
    System.out.println(st1.equals(st2));
  }

}

Output: first line: true second line: true analysis: first look at the first print statement, in Java == this symbol is a comparison operator, it can be the basic data type and reference data type are equal, if it is basic For data types, == compares whether the values ​​are equal, and if it is a reference data type, == compares whether the memory addresses of the two objects are equal. The string does not belong to the basic data type in 8, the string object belongs to the reference data type, "abc" is assigned to the two string objects st1 and st2 at the same time, pointing to the same address, so the first print The == comparison output in the statement is true. Then we look at the comparison of equals in the second print statement. We know that equals is a method of the parent class of Object. This equals method is rewritten in the String class. In the JDK API 1.6 Find the equals method under the String class in the document, click to see the sentence "Compare this string with the specified object. If and only if the parameter is not null, and it is the object that represents the same character sequence as this  object The result is then . "Note that this same sequence of characters, the comparison of two arrays, lists, and dictionaries introduced later will be implemented by this logic to write code. Since the values ​​of st1 and st2 are both "abc", the two point to the same object, and the current character sequence is the same, so the second line print result is also true. Let's draw a memory graph to represent the above code, which looks more convincing. String true

The memory process is roughly as follows: 1) Run first to compile, and then load the current class Demo2_String.class file into the method area of ​​memory 2) The second step, the main method is pushed into the stack memory 3) The constant pool creates an "abc" object and generates a memory Address 4) Then assign the "abc" memory address to the member variable st1 in the main method. At this time, st1 points to "abc" in the constant pool according to the memory address. 5) As mentioned in the previous article, the constant pool has this feature. If it is found to exist, it will not create duplicate objects. 6) Run the code Stringst2 = "abc", because the constant pool has "abc", so it will not be created again. Directly assign the "abc" memory address to st27) Finally, both st1 and st2 point to the same address in memory, so the two are identical. 2. The following sentence creates several objects in memory. The answer is: create two objects in memory, one in the heap memory and one in the constant pool. The heap memory object is a copy copy of the constant pool object. Analysis: Let's take a memory map directly below.

When we see the keyword new, we must think that the objects that come out of new are all stored in the heap memory. Then we explain why the objects in the heap are copy copies of objects in the constant pool. "Abc" is a string and the string is a constant, so it should be created in the constant pool, so the first object created is "abc" in the constant pool. The second object is a copy of the copy in the heap memory. This requires a note on the constructor of String (String original) in JDK API 1.6: initialize a newly created String object so that it represents a parameter with the same Character sequence; in other words, the newly created string is a copy of the parameter string. So, the answer came out, two objects.

3. Determine whether the following st1 and st2 defined as String types are equal

package string;
public class Demo2_String {
   public static void main(String[] args) {
     String st1 = new String("abc");
     String st2 = "abc";
     System.out.println(st1 == st2);
     System.out.println(st1.equals(st2));
   }
}


Answer: false and true Because of the previous two experiences and theories on memory analysis, I can quickly get the above answer. == Compare the memory addresses of the st1 and st2 objects. Since st1 points to the address of the heap memory, st2 sees that “abc” already exists in the constant pool, so it will not be created again, so st2 points to the memory address of the constant pool, So == judgment result output false, the two are not equal. The second equals comparison, the comparison is whether the two string sequences are equal, because there is only one "abc", so they are completely equal. The memory map is as follows

4. Determine whether the following st1 and st2 defined as String types are equal

package string;
 
public class Demo2_String {
 
   public static void main(String[] args) {
     String st1 = "a" + "b" + "c";
     String st2 = "abc";
     System.out.println(st1 == st2);
     System.out.println(st1.equals(st2));
   }
}

The answer is: true and true analysis: "a", "b", "c" are originally string constants, after the + sign is spliced, it becomes "abc", and "abc" itself is a string constant (in Java There is a constant optimization mechanism), so the constant pool will immediately create an "abc" string constant object, st2 = "abc", at this time, the constant pool exists "abc", so it is no longer created. So, whether you compare memory addresses or compare string sequences, they are all equal. 5. Determine whether the following st2 and st3 are equal

package string;
 
public class Demo2_String {
 
   public static void main(String[] args) {
     String st1 = "ab";
     String st2 = "abc";
     String st3 = st1 + "c";
     System.out.println(st2 == st3);
     System.out.println(st2.equals(st3));
   }
}

Answer: false and true analysis: the first answer is false, the second is true, and the second is true. We understand very well, because the comparison is "abc", and the other is "abc" obtained by stitching. So equals comparison, this is output true, we understand very well. So the first judgment is why it is false, we are very puzzled. Similarly, below we use API annotations and memory maps to explain why this is not equal. First, open the introduction of String in JDK API 1.6 and find the sentence in the picture below.

The key point is the sentence in the red circle. We know that any data and strings are plus (+), and the result is a new string. The above comment shows that the principle of this splicing is achieved by the StringBuilder or StringBuffer class and the append method inside, and then toString () is called to convert the spliced ​​object into a string object, and finally the address of the string object is assigned to a variable. Combined with this understanding, we draw a memory graph to analyze.

Rough memory process
1) The constant pool creates an "ab" object and assigns it to st1, so st1 points to "ab" 2) The constant pool creates an "abc" object and assigns it to st2, so st2 points to "abc" 3) The + stitching method taken here, so the third step is to use the append method of the StringBuffer class to get "abc". At this time, the memory 0x0011 represents a StringBuffer object. Note that it is not a String object. 4) Called the toString method of Object to replace the StringBuffer object with a String object. 5) Assign the String object (0x0022) to st3. Therefore, the st = and st2 == judgment results are not equal, because the memory addresses of the two objects are different. Summary: This interview question completely requires mastering some of the annotations and principles in the JDK API, as well as the analysis of the memory map to get the correct results. I admit that drawing the memory map allows me to understand why the answer is this way. After drawing the memory map, get the answer, you will find it very interesting, and finally there will be such a lament.

Guess you like

Origin www.cnblogs.com/xiaogeng88/p/12692266.html