Home > Java > JavaBase > How to determine whether two objects are equal in java

How to determine whether two objects are equal in java

Release: 2019-11-22 11:00:31
Original
6230 people have browsed it

How to determine whether two objects are equal in java

In Java, you can use the equals() method to determine whether two objects are equal. The equals() method is used to compare a string with a specified object. Returns true if the given object is equal to the string; false otherwise.

The source code of this method is as follows:

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

All objects have identification (memory address) and status (data), and "==" compares the memory addresses of the two objects, so It is said that using the equals() method of Object is to compare whether the memory addresses of two objects are equal. That is, if object1.equals(object2) is true, it means that equals1 and equals2 actually refer to the same object.

Although sometimes the equals() method of Object can meet some of our basic requirements, we must be aware that most of the time we compare two objects. At this time, the equals() method of Object That's not possible. In fact, in the JDK, encapsulation classes such as String and Math all rewrite the equals() method.

The following is the equals() method of String:

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

For this code segment: if (v1[i] != v2[j]) return false; we can see very clearly String's equals() method performs content comparison, not reference comparison. As for other encapsulation classes, they are similar.

In the Java specification, its use of the equals() method must follow the following rules:

The equals method achieves equality on non-null object references :

1. Reflexivity: For any non-null reference value x, x.equals(x) should return true.

2. Symmetry: For any non-null reference values ​​x and y, x.equals(y) should return true if and only if y.equals(x) returns true.

3. Transitivity: For any non-null reference values ​​x, y and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) Should return true.

4. Consistency: For any non-null reference values ​​x and y, calling x.equals(y) multiple times always returns true or always returns false, provided that the information used in the equals comparison on the object has not been Revise.

5. For any non-null reference value x, x.equals(null) should return false.

For the above rules, it is best to abide by them during use, otherwise unexpected errors will occur.

When comparing in java, we need to choose the appropriate comparison method according to the type of comparison:

1) In the object field, use the equals method.

2) Type-safe enumeration, use equals or ==.

3) Possibly null object fields: use == and equals.

4) Array field: use Arrays.equals.

5) Primitive data types except float and double: use ==.

6) Float type: Use Float.foatToIntBits to convert to int type, and then use ==.

7) Double type: Use Double.doubleToLongBit to convert to long type, and then use ==.

For more java knowledge, please pay attention to java basic tutorial.

The above is the detailed content of How to determine whether two objects are equal in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template