common problem of class

2023-03-14  

public class StringDemo01 {
    
    public static void main(String[] args) {
    
        String string01 = "zrt";
        String string02 = "zrt";
        System.out.println(string01 == string02);		//true
        System.out.println(string01.equals(string02));	//true
    }
}

Analysis:

  1. In Java, the symbol comparative operator “==” can be used for comparisonBasic data typeand harmonyReference data typeWhether it is equal

    • Basic data type: Comparison is

      Whether the value ofis equal

    • Reference Data Type: CompareWhether the memory address of the two objects is equal

    • Note that the String type does not belong to eight basic data types. In other words, the String object belongs to the reference data type. The “ZRT” is assigned to the two String types at the same time, and the references of the two String classes are pointing to the same address, so the first statement is “TRUE”.

  2. Object class provided by the Equals () method provided in the String class (inherited the Object class) method rewritten the EQUALS () method. It can be seen through the JDK document that this method is interpreted as “compares this string with the specified object. OnlyThis parameter is not NULL, and it is represented by this objectThe same character sequenceString object, the result istrue. “Because the values of String01 and Strin02 are” ZRT “, which hasThe same character sequence, so the second statement is “true”.

  3. The memory diagram of the above example is as follows:

This process is roughly as follows:

  • Run first compile, load the current class StringDemo01.class file into the method area of the memory.

    The

  • main method is pressed into the stack memory.
  • The constant pool creates a “ZRT” object to generate a memory address.
  • and then assign the “ZRT” memory address to the member variables String01 in the Main method. At this time, String01 points to the “ZRT” in the constant pool according to the memory address.
  • The constant pool has a characteristic. It is found that the string object “ZRT” already exists, and it is not to create repeated objects.
  • Run to the code string string02 = “ZRT”. Since the “ZRT” exists in the constant pool, the “ZRT” memory address is assigned to String02.
  • So, String01 and String02 both points to the same address in memory, so the two are exactly the same.

public class StringDemo02 {
    
    public static void main(String[] args) {
    
        String string01 = "zrt";
    }
}

How many objects have the above example created in memory?

The answer is: two, one pile of memory, one in the constant pool, andPacking memory object is a copy of the copy of the constant pool

Analysis: We all know that the keywords of NEW, the objects of the news are stored in the heap memory. “ZRT” belongs to the string, and the string belongs to the constant, so it should be created in the constant pool, so the first object created is “ZRT” in the constant pool. So why is the pile memory object a copy of a copy of the constant pool? From the document of JDK1.6, you can see that the constructor String (String Original) comments: “Initialize a new creationStringObject, so that it represents a character sequence of the same parameter “. That is to say, the newly created string is the copy of the parameter string. In other wordsPacking memory object is a copy of the copy of the constant pool

The memory diagram of the above example is as follows:


public class StringDemo03 {
    
   public static void main(String[] args) {
    
     String string01 = new String("zrt");
     String string02 = "zrt";
     System.out.println(string01 == string02);		//false
     System.out.println(string01.equals(string02));	//true
   }
}

Analysis: “==” Comparison String01 object and memory address of the String02 object. Since String01 points to the address of the heap memory, when String02 sees that “ZRT” already exists in the constant pool, it directly points to the memory address of the constant pool’s memory address So, the output result of the first statement is FALSE. The second EQUALS () placing method is compared. The comparison is whether the two string sequences are equal. Since they are “ZRT”, the output result of the second statement is true.

The memory diagram of the above example is as follows:


public class StringDemo04 {
    
   public static void main(String[] args) {
    
     String string01 = "z" + "r" + "t";
     String string02 = "zrt";
     System.out.println(string01 == string02);		//true
     System.out.println(string01.equals(string02));	//true
   }
}

Analysis: “Z”, “R”, “T” was originally the three string constant volume. After “+” symbols are stitched, it becomes a string “ZRT”, and “ZRT” itself is the string constant ( Java has a constant optimization mechanism), so it will create a “ZRT” string constant object in the constant pool. Therefore, when you go to String02 = “ZRT”, because the constant pool has “ZRT”, the “ZRT” string object will no longer be created. Therefore, whether it is a comparative memory address or a string sequence, they are equal, so the output results of the two statements are TRUE.


public class StringDemo05 {
     
   public static void main(String[] args) {
    
     String string01 = "zrt";
     String string02 = "zrt";
     String string03 = string01 + "t";
     System.out.println(string02 == string03);		//false
     System.out.println(string02.equals(string03));	//true
   }
}

Among them, the output of the second statement is TRUE, which will not be repeated here. Mainly why is the output result of the first statement? First of all, we can see such a sentence in the documentation of JDK API1.6:

java language provides special support for the string series connection symbols (“+”) and converting other objects into string. The string series is passedStringBuilder(orStringBuffer) Class and itappendMethod implemented. String conversion is passed

ThetoStringmethod is implemented, this method is

ClassObjectdefinition and can be inherited by all classes in Java.

Analysis: From the above, we can know that any data and strings are performed (+) operations, and finally obtained a new string stitching. The above comment shows that the principle of this stitching is to implement stitching by StringBuilder or StringBuffer class and the APPEND method in it, and then call Tostring () to convert the stitching object into a string object, and finally assign the memory address of the string object to the variable.

The memory diagram of the above example is as follows:

The process of

is roughly as follows:

  • The “ZR” object is created and the memory address value is assigned to String01, so String01 points to “ZR”.
  • The “ZRT” object is created and the memory address value is assigned to String02, so String02 points to “ZRT”.
  • Since the statement uses “+” for stitching String01 and the string object “T”, it uses the APPEND () method of the StringBuffer class to get the StringBuffel object “ZRT” (Note: Note: It’s not String Object), use it. 0x01 means memory address.
  • Call the Tostring () method of the Object class, convert the StringBuffer object into a String object, and use 0x01 to represent the memory address.
  • The memory address (0x02) of the String object is assigned to String03. So the output result of the “==” judgment is false, because the memory address is different.

This article is that I encountered a small doubt when I was studying the String class, and made a note to deepen the impression. The point is to understand some of the comments and principles and memory charts of JDK API. By drawing memory maps, we can also help us understand the principles.


source

Random Posts

Configure the error cvc-complex-type.2.4.a when configured Springmvc

HDOJ 1880 spell dictionary

[Reading book records] The accumulation of knowledge and persistence;

#Pragma comments AUTHUR

[rabbitmq] Getting Start (Super Details) Jorgen