The difference between equals () and "==" in

Disclaimer: This article is a blogger original article, please indicate the source: https://blog.csdn.net/qq_38182125/article/details/89597621

I. Overview

In Java, equals () method and == we often use, is often one of the questions in the interview. This article will analyze the differences between both of them.


Second, on the "=="

== comparison can be divided into the following two:

  • Comparison of basic data types
  • Comparative data type references

1. Comparison of basic data types

When using the "==" basic data types are compared, their values ​​are compared, such a comparison between the comparison values ​​are all variable and Comparative int int type variables, boolean boolean variables and variables. These are our usual frequently used the example given here is not.

2. The reference comparative data types

If the object compared to reference data types, then the comparison is their memory addresses. At this time, "==" symbol for determining whether the referenced equal on both sides the same object, the following example:

public class Main {

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

result:

false

Since a, b are referenced heap different objects, so they use "==" Analyzing the return value is false, that is not the same object.


Three, equals () method

equals()Method is in Objectthe class, each class there will be a method of its default codes are as follows:

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

From its internal implementation, we can also see that the default memory address it and "==", as is the case of comparing two objects. But this awareness and we usually differ from the following code:

public class Main {

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

This code will return to our usual use experience true, the fact that it will return true, even like this we will often use a way to judge the case whether the string is empty. So we have to see the implementation of the String equals () method is to look like:

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

We can see from the above code, String is actually a rewrite equals () method, when the judgment is not the same object when, equals method will be followed by their value, it will return true if fully equal, so after the equals String () method to rewrite the equivalent function is a comparison value, and this is our usual habits consistent.

From here we can know, when we define a class, if we need to compare two objects of that type values ​​are equal, we need to rewrite equals () method, and from the definition of equal conditions and rules, whereby achieve value comparison.


Fourth, the digression of hashCode ()

The reason for said here about hasCode () method because it equals () method are closely related. hashCode () method for calculating the hash value of an object, wherein a is an integer hash value of 32bit.

Java stipulated: must be) consistent for each data type hashCode () method equals (Method. In other words, a.equals (b) returns true, then a.hashCode () return value and necessity b.hashCode () returns the same value. Conversely, if the two objects hashCode () method return values ​​are different, then we know that the two objects are different. However, if the return value of the two objects hashCode () method of the same, these two objects may also be different, we also need to be determined using the equals method.

The above agreement: If we need to rewrite the custom class hashCode () method, then we need to override the equals () method. The default hashCode () will return the memory address of the object, but this only applies to a few cases. Java rewrite this method for many common data types (String, Integer, Double, File, and URL).


I hope this article helpful to you ~

Guess you like

Origin blog.csdn.net/qq_38182125/article/details/89597621