Object--equal()

equals()

 1 /**
 2      * Indicates whether some other object is "equal to" this one.
 3      * <p>
 4      * The {@code equals} method implements an equivalence relation
 5      * on non-null object references:
 6      * <ul>
 7      * <li>It is <i>reflexive</i>: for any non-null reference value
 8      *     {@code x}, {@code x.equals(x)} should return
 9      *     {@code true}.
10      * <li>It is <i>symmetric</i>: for any non-null reference values
11      *     {@code x} and {@code y}, {@code x.equals(y)}
12      *     should return {@code true} if and only if
13      *     {@code y.equals(x)} returns {@code true}.
14      * <li>It is <i>transitive</i>: for any non-null reference values
15      *     {@code x}, {@code y}, and {@code z}, if
16      *     {@code x.equals(y)} returns {@code true} and
17      *     {@code y.equals(z)} returns {@code true}, then
18      *     {@code x.equals(z)} should return {@code true}.
19      * <li>It is <i>consistent</i>: for any non-null reference values
20      *     {@code x} and {@code y}, multiple invocations of
21      *     {@code x.equals(y)} consistently return {@code true}
22      *     or consistently return {@code false}, provided no
23      *     information used in {@code equals} comparisons on the
24      *     objects is modified.
25      * <li>For any non-null reference value {@code x},
26      *     {@code x.equals(null)} should return {@code false}.
27      * </ul>
28      * <p>
29      * The {@code equals} method for class {@code Object} implements
30      * the most discriminating possible equivalence relation on objects;
31      * that is, for any non-null reference values {@code x} and
32      * {@code y}, this method returns {@code true} if and only
33      * if {@code x} and {@code y} refer to the same object
34      * ({@code x == y} has the value {@code true}).
35      * <p>
36      * Note that it is generally necessary to override the {@code hashCode}
37      * method whenever this method is overridden, so as to maintain the
38      * general contract for the {@code hashCode} method, which states
39      * that equal objects must have equal hash codes.
40      *
41      * @param   obj   the reference object with which to compare.
42      * @return  {@code true} if this object is the same as the obj
43      *          argument; {@code false} otherwise.
44      * @see     #hashCode()
45      * @see     java.util.HashMap
46      */
47     public boolean equals(Object obj) {
48         return (this == obj);
49     }
View Code

 

表示其他对象是不是和本对象“相等”

 

equals方法实现非空对象引用的等价关系

 

equals方法具有自反性:对于任何非空引用x,x.equals(x)应该返回true

equals方法具有对称性:对于任何非空引用x和y;x.equals(y)返回true,那么y.equals(x)也返回true

 

equals方法具有传递性:对于任何非空引用x,y和z;

x.equals(y)返回true;y.equals(z)也返回true,那么x.equals(z)也返回true

 

equals方法具有一致性:对于任何非空引用x和y,倘若没有任何用于equals方法比较的信息被修改,多次调用x.equals(y)应该一直返回true或者一直返回false

对于任何非空引用x,x.equals(null)一定返回false 

 

Object的equals方法实现了对象之间最有辨别力的可能的等价关系,也就是说,对于任何非空对象x和y,当且仅当x和y引用同一个对象时这个方法返回ture(x == y 值为 true)

 

需要注意的是:无论何时equals方法被重写时,重写hashCode方法很重要。这样以便维持普遍约定:相等的对象必须有相等的哈希码

posted @ 2022-08-02 10:52  JustJavaIt  阅读(82)  评论(0)    收藏  举报