Talking about the == and equals() methods in Java

== operator

There are two ways to measure whether two variables are equal in Java: the first is to use the == operator, and the second is to use the equals() method.

  1. Basic data types
    Use == to judge whether variables are equal. When the variables are of four types and eight basic data types, and they are all numeric types, as long as the values ​​of the two variables are the same, it can return true.
public class Test {
    
    
    public static void main(String[] args) {
    
    
        char a='A';
        int b =65;
        double c=65.0d;
        System.out.println(a==b);//end=true
        System.out.println(b==c);//end=true
        System.out.println(a==c);//end=true
    }

}

In the code above, the ascii code value of the char character a is 65, so use the == operator to judge the three variables of abc to get true.

  1. Reference type

For reference type variables, it can only be used when the two variables are of the same type or have a parent-child relationship, otherwise an error will be reported. When two reference type variables use the == operator, only when they point to the same object will it return true.

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

In the program above, the values ​​of variable a and variable b are both abc, but the references of a and b are different, so false is returned.

String constant pool

For novices, String has many very confusing places: What is the difference between "abc" and new String("abc")? To solve this problem, we need to understand the Java string constant pool mechanism.
The location of the
string constant pool The string constant pool is located in the method area. When creating a string, first check whether the string exists in the constant pool. If it exists, it will directly return a reference to the string. If it does not exist, it will be in the string constant Create the string in the pool and return a reference. The JVM constant pool guarantees that there is only one direct quantity of the same string and will not produce multiple copies.

   String a="我爱学习";
   String b="我爱"+"学习";
   String c="学习";
   final String d="学习";
   String e="我爱"+c;
   String f="我爱"+d;
   System.out.println(a==b);//end=true,因为b在编译阶段可以确定下来,
   //因此b直接返回字符串常量池中的引用,所以相等。
   System.out.println(a==e);//变量e="我爱"+c;变量c在编译时不能确定下来,因此返回false
   System.out.println(a==f);
  /*end=true 变量d加入final修饰符,final会告诉编译器变量d这个数据不会修改,在编译时直接将变量d替换成"学习",因此编译时变量f可以确定下来为"我爱学习",直接返回字符串常量池的引用,故a==f
  */

When using new String("abc"), the JVM virtual machine first checks whether there is "abc" in the constant pool, and creates the "abc" string if it does not exist, and then calls the constructor of the String class to create one in the heap memory The new String object has a value of abc and returns a reference to the object in the heap memory. Simply put, using new String() will create two objects.

String a=new String("abc");
String b="abc";
System.out.println(a==b);//end=false

In the above figure, when declaring a variable, the JVM first creates an "abc" object in the string constant pool, then creates an "abc" object in the heap memory, and then returns the reference to the object in the heap memory to a. When the variable b is declared, the JVM finds "abc" in the string constant pool and directly returns the reference of the object in the constant pool to b. The contents of variable a and variable b are the same, but one is a reference to the constant pool and the other is a reference to the heap memory. Therefore the result of a==b is false

equals() method

The equals() method is an instance method provided by the Object class. It is used to judge whether two reference type variables are equal. You can find by clicking the source code of the Object class. This method judges whether two objects are equal and the == operator has no difference, so This method of the Object class has little meaning, and we often override this method in actual development.

//Object类源码
public boolean equals(Object obj) {
    
    
        return (this == obj);
    }

The String class overrides the equals() method, as long as the sequence of the two strings is the same, it can return true. Therefore, the running results in the following code are all true.

   String a="我爱学习";
   String b="我爱"+"学习";
   String c="学习";
   final String d="学习";
   String e="我爱"+c;
   String f="我爱"+d;
   System.out.println(a.equals(b));//end=true
   System.out.println(a.equals(e));//end=true
   System.out.println(a.equals(f));//end=true

In actual development, we usually need to rewrite the equals method. When rewriting the method, the equal conditions are determined by the business requirements, so the realization of the equals() method is also determined by the business requirements.
Generally speaking, the rewriting quals() party should meet the following conditions:

  • Reflexivity: For any non-null reference value x, x.equals(x) should return true.
  • Symmetry: For any non-null reference value x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • Transitivity: For any non-null reference value x, y, and z, if x.equals(y) returns true, and y.equals(z) returns true, then x.equals(z) should return true.
  • Consistency: For any non-null reference values ​​x and y, multiple calls to x.equals(y) always return true or always return false, provided that the information used in the equals comparison on the object has not been modified.
  • For any non-null reference value x, x.equals(null) should return false.

Guess you like

Origin blog.csdn.net/ln82799/article/details/109149947