Explain the "==" and "equals" in Java

1. “ = = ” “==” =="
For"==" "==" inJava=="In comparison, if the basic data type is compared, the value of the basic data type is compared (Note: The data types on both sides of the equal sign do not have to be the same, as long as the value is the same, it can return true, but for a certain Comparing some different data types, the compiler will report an error directly)

 public static void main(String[] args) {
    
    
        int a=1;
        int b=1;
        char c=1;
        byte d=1; //byte数据类型:代表一个字节(8位:-128~127)
        boolean e=true;
        System.out.println(a==b);//true
        System.out.println(a==c);//true
        System.out.println(a==d);//true
        System.out.println(a==e);//报错
    }

The basic data types in Java are:
1. Logic type: boolean
2. Text type: char
3. Integer type: byte, short, int, long
4. Floating point type: double, float.

In other words, for the above data types, "= =" "=="==" All values ​​are compared. True is returned if the value is equal, and false if the value is not equal.

If the comparison is not a basic data type, but a reference type, then "= =" "=="==" The comparison is not the value of their "content", but the value of the memory addresses of the two. If the memory addresses of the two are the same, it returns true, otherwise it returns false

 public static void main(String[] args) {
    
    
        Object a=new Object();
        Object b=new Object();
        Object c= a;
        System.out.println(a==b);//false
        System.out.println(a==c);//true

    }

The reference types in Java are:
1. Class: class
2. Interface: Interface
3. Array: Array

Record a giant hole in the String class:

 public static void main(String[] args) {
    
    
       String a="123";
       String b="123";
       String c=new String("123");
       String d=new String("123");
       System.out.println(a==b); //ture
       System.out.println(a==c); //false
       System.out.println(c==d); //false
    }

Why does it appear that the values ​​of the strings a, b, c, and d are all equal, but == ===The result of = sign comparison is that some are equal, but some are different. This involves some underlying principles of Java. We know that there are two ways to create a String object, one is String a="123"; the other is String c=new String("123"); if the object is created in the first way, the object is created In the constant pool of Java, the specific method of creation is that the Java virtual machine now finds whether there is an object "123" in the constant pool, if it exists, it will directly return a reference to "123", if it does not exist, create an object "123" ", and then return its reference, so in fact, a and b are references to the same object "123"; and if the object is created in the second way, the object is created and then in the heap, and the virtual machine is created in the heap An object, and then find the object "123" in the constant pool, if not, create one in the constant pool, and then copy the object "123" in the constant pool to the newly created object in the heap, and then return the object in the heap Reference, so the objects referenced by c and d are in the heap and they are different objects. Obviously, the object addresses of c and d are different while the object addresses of a and b are the same.
Insert picture description here

2.
equals () The prototype of the equals method is in the class Object, and the Object class is the parent class of all classes. If we look at the source code of the equals method in the Object class, we will find that there is only one line of code

Insert picture description here
And we can see the equal method and == == in the Object class== Is the same. It is still address comparison for classes, but the equals method has been rewritten in many other classes so that it is no longer an address comparison, such as the String class.
Insert picture description here
For equals() in the String class, if the addresses are the same, it returns true. If the addresses are different, the equals() method in the StringLatin1 class or the equals() method in the StringUTF16 will be called.

Let's take a look at the equals() method in the StringLatin1 class (the principle in the StringUTF16 class is similar)

Insert picture description here

Obviously, you can see that the equals() method in the StringLatin1 class compares strings character by character, so after the equals() method is rewritten in the String class, it is no longer an address comparison, but a value comparison, so we It is best not to use == == when comparing two strings for equality== , But use the equals method.

public class Main{
    
    
    public static void main(String[] args) {
    
    
        String a=new String("123");
        String b=new String("123");
        System.out.println(a==b);//false
        System.out.println(a.equals(b));//true
    }
}

Guess you like

Origin blog.csdn.net/CY2333333/article/details/107968005