在Java中使用带有String和Object的equals()方法 [英] Using the equals() method with String and Object in Java

查看:227
本文介绍了在Java中使用带有String和Object的equals()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Object o1 = new Object();
Object o2 = new Object();
//o1=o2;
System.out.println(o1.equals(o2));

它返回 false 。如果评论被删除,它可以返回 true

It returns false. It can return true, if the comment is removed.

为什么不适用于 String 类?

String s1=new String();
String s2=new String();
System.out.println(s1.equals(s2));

它返回 true 。为什么? (因为字符串使用实习或其他相关内容?)

It returns true. Why? (because String uses interns or something else involved?)

推荐答案

因为 字符串比较内容,而不是对象本身。

Because equals() for String compares the content, not the object itself.


public boolean equals(Object anObject)

public boolean equals(Object anObject)

将此字符串与指定的字符串进行比较宾语。当且仅当参数不为null并且是 String 对象时,结果才为真,该对象表示与此对象相同的字符序列。

Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.



    /* String.equals() */
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;
}

(链接到 String .equals()

equals for Object

Versus the equals for Object:


Object 的equals方法实现了对象上最具辨别力的等价关系;也就是说,对于任何非空引用值 x y ,当且仅当<$时,此方法返回true c $ c> x 和 y 引用同一个对象( x == y true )。

The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).



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

(链接到 Object .equals()

另外,不要忘记的合约等于()函数:


equals方法在非空对象引用上实现等价关系:

The equals method implements an equivalence relation on non-null object references:


  • 自反:对于任何非空参考值 x x .equals(x)应该返回true。

  • 对称:对于任何非空引用值 x y x.equals(y)当且仅当 y.equals(x)返回true。

  • 传递:对于任何非空引用值 x y z ,如果 x.equals(y)返回 true y.equals(z)返回 true ,然后 x.equals(z)应返回 true

  • 一致:对于任何非空参考值 x y ,多次调用 x.equals(y)始终返回 true 或一致返回 false ,前提是未修改对象的等比较中使用的信息。

  • 对于任何非空引用值 x x.equals(null)应该返回 false

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: 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.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

还建议阅读:

  • Object.hashCode()
  • Effective Java (Bloch)

这篇关于在Java中使用带有String和Object的equals()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆