Java equals的一個坑

來源:互聯網
上載者:User

標籤:

 

public class StringEqualsObject {    public static void main(String[] args) {        String name="Tom";        Person p=new Person(name);        System.out.println(name.equals(p));        System.out.println("p.toString():"+name.equals(p.toString()));        System.out.println(p.equals(name));    }}class Person{    private String name;    public Person(String name) {        super();        this.name = name;    }    @Override    public String toString() {        return this.name;    }}

 

Output:

falsep.toString():truefalse

原因:
java.lang.String

  /**     * Compares this string to the specified object.  The result is {@code     * true} if and only if the argument is not {@code null} and is a {@code     * String} object that represents the same sequence of characters as this     * object.     *     * @param  anObject     *         The object to compare this {@code String} against     *     * @return  {@code true} if the given object represents a {@code String}     *          equivalent to this string, {@code false} otherwise     *     * @see  #compareTo(String)     * @see  #equalsIgnoreCase(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;    }

java.lang.Object

 

/**     * Indicates whether some other object is "equal to" this one.     * <p>     * The <code>equals</code> method implements an equivalence relation     * on non-null object references:     * <ul>     * <li>It is <i>reflexive</i>: for any non-null reference value     *     <code>x</code>, <code>x.equals(x)</code> should return     *     <code>true</code>.     * <li>It is <i>symmetric</i>: for any non-null reference values     *     <code>x</code> and <code>y</code>, <code>x.equals(y)</code>     *     should return <code>true</code> if and only if     *     <code>y.equals(x)</code> returns <code>true</code>.     * <li>It is <i>transitive</i>: for any non-null reference values     *     <code>x</code>, <code>y</code>, and <code>z</code>, if     *     <code>x.equals(y)</code> returns <code>true</code> and     *     <code>y.equals(z)</code> returns <code>true</code>, then     *     <code>x.equals(z)</code> should return <code>true</code>.     * <li>It is <i>consistent</i>: for any non-null reference values     *     <code>x</code> and <code>y</code>, multiple invocations of     *     <tt>x.equals(y)</tt> consistently return <code>true</code>     *     or consistently return <code>false</code>, provided no     *     information used in <code>equals</code> comparisons on the     *     objects is modified.     * <li>For any non-null reference value <code>x</code>,     *     <code>x.equals(null)</code> should return <code>false</code>.     * </ul>     * <p>     * The <tt>equals</tt> method for class <code>Object</code> implements      * the most discriminating possible equivalence relation on objects;      * that is, for any non-null reference values <code>x</code> and     * <code>y</code>, this method returns <code>true</code> if and only     * if <code>x</code> and <code>y</code> refer to the same object     * (<code>x == y</code> has the value <code>true</code>).     * <p>     * Note that it is generally necessary to override the <tt>hashCode</tt>     * method whenever this method is overridden, so as to maintain the     * general contract for the <tt>hashCode</tt> method, which states     * that equal objects must have equal hash codes.      *     * @param   obj   the reference object with which to compare.     * @return  <code>true</code> if this object is the same as the obj     *          argument; <code>false</code> otherwise.     * @see     #hashCode()     * @see     java.util.Hashtable     */    public boolean equals(Object obj) {    return (this == obj);    }

 

Java equals的一個坑

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.