The difference between java equals and ==

  • 2020-05-12 02:34:44
  • OfStack

What does equals and == compare to:

1. boolean tem = a == b;

First of all, == must compare the address, from the point of view of the stack, that is to say, == compares the contents on the stack. Because the stack is used to store the address or the literal value of the automatic variables in the eight basic types in java (the automatic variables are int a = 1; This form defines the variable). If the value of the automatic variable comparison is to be used, it must be ==, because equals() is a method, so it must be called by the object to be used for comparison. An automatic variable is neither an instance of a class nor a reference to a class, so the equals() method cannot be used.

2. boolean tem = a. equals (" b ");

The equals() method 1 is usually used to compare the contents of an object, but in some cases it also compares the addresses of two objects.

The following

Write a test program


package com;

import java.util.Date;

public class test {
  public static void main(String[] args) {
    Integer integer1 = new Integer(1);
    Integer integer2 = new Integer(1);
    String str1 = new String("123");
    String str2 = new String("123");
    Date date1 = new Date();
    Date date2 = new Date();
    Double double1 = new Double("1.0");
    Double double2 = new Double("1.0");
    Boolean tem1 = new Boolean(true);
    Boolean tem2 = new Boolean(true);
    Object object1 = new Object();
    Object object2 = new Object();

    System.out.println("----Object------");
    System.out.println(object1.equals(object2));
    System.out.println(object1 == object2);
    System.out.println(object1.equals(object1));
    System.out.println(object1 == object1);
    System.out.println("----Boolean------");
    System.out.println(tem1.equals(tem2));
    System.out.println(tem1 == tem2);
    System.out.println("----Double------");
    System.out.println(double1.equals(double2));
    System.out.println(double1 == double2);
    System.out.println("----Integer------");
    System.out.println(integer1.equals(integer2));
    System.out.println(integer1 == integer2);
    System.out.println("----String------");
    System.out.println(str1.equals(str2));
    System.out.println(str1 == str2);
    System.out.println("----Date------");
    System.out.println(date1.equals(date2));
    System.out.println(date1 == date2);
  }
}
 

Results:

----Object------
false
false
true
true
----Boolean------
true
false
----Double------
true
false
----Integer------
true
false
----String------
true
false
----Date------
true
false

First of all, Object is compared. When two objects are 1 sample, the results of == and equals() are both true. When two objects are not 1 sample, false is returned. Therefore, when == and equals are used to compare objects, what they compare is the address of the object, which is essentially the same. Here is the code for the equals() method in the Object class:


  public boolean equals(Object obj) {
    return (this == obj);
  }
  

For Boolean,Double(Float),Interger(Shot,Long),String,Date (Shot,Long),String,Date,String,Date), equals(), Boolean, Double, Interger, String, Date.

Boolean:


public boolean equals(Object obj) {
    if (obj instanceof Boolean) {
      return value == ((Boolean)obj).booleanValue();
    }
    return false;
  }

Double:


public boolean equals(Object obj) {
    return (obj instanceof Double)
        && (doubleToLongBits(((Double)obj).value) ==
           doubleToLongBits(value));
  }

Interger:


public boolean equals(Object obj) {
    if (obj instanceof Integer) {
      return value == ((Integer)obj).intValue();
    }
    return false;
  }

String:


public boolean equals(Object anObject) {
    if (this == anObject) {
      return true;
    }
    if (anObject instanceof String) {
      String anotherString = (String) anObject;
      int n = value.length;
      if (n == anotherString.value.length) {
        char v1[] = value;
        char v2[] = anotherString.value;
        int i = 0;
        while (n-- != 0) {
          if (v1[i] != v2[i])
              return false;
          i++;
        }
        return true;
      }
    }
    return false;
  }

Date:


public boolean equals(Object obj) {
    return obj instanceof Date && getTime() == ((Date) obj).getTime();
  }

That is to say, the equals() method of Object class is overridden at these times to compare the actual content of two objects instead of the address. Of course, it is not only these, but a few examples of common java native class overrides equals() method of Object class.

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: