A pit of Java equals

Source: Internet
Author: User

 Public classStringequalsobject { Public Static voidMain (string[] args) {String name= "Tom"; Person P=NewPerson (name);        System.out.println (Name.equals (p)); System.out.println ("P.tostring ():" +name.equals (p.tostring ()));    System.out.println (p.equals (name)); }}classperson{PrivateString name;  PublicPerson (String name) {Super();  This. Name =name; } @Override PublicString toString () {return  This. Name; }}

Output:

false p.tostring (): true false

Reason:
Java.lang.String

  /*** Compares this string to the specified object. The result is {@code* true} if and only if the argument are not {@codenull} and is a {@code* String} object that represents the same sequence of characters as this * object. *     * @paramAnObject * The object to compare this {@codeString} against * *@return  {@codetrue} If the given object represents a {@codeString} * equivalent to the This String, {@codefalse} otherwise * *@see#compareTo (String) *@see#equalsIgnoreCase (String)*/     Public Booleanequals (Object anobject) {if( This==anobject) {        return true; }    if (anobject instanceof String) { string anotherstring=(String) AnObject; intn =count; if(n = =anotherstring.count) {CharV1[] =value; CharV2[] =Anotherstring.value; inti =offset; intj =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 R     Eferences: * <ul> * <li>it is <i>reflexive</i>: For any non-null reference value *     <code>x</code>, <code>x.equals (x) </code> should return * &LT;CODE&GT;TRUE&LT;/CODE&GT;.  * <li>it is <i>symmetric</i>: non-null Reference values * <code>x</code> and <code>y</code>, <code>x.equals (y) </code> * should return &LT;CODE&GT;TRUE&LT;/CODE&GT ;     If and only if * <code>y.equals (x) </code> returns &LT;CODE&GT;TRUE&LT;/CODE&GT;. * <li>it is <i>transitive</i>: For any non-null reference values * &LT;CODE&GT;X&LT;/CODE&GT; Lt;code>y</code>, and <code>z</code>, if * <code>x.equalS (y) </code> returns <code>true</code> and * <code>y.equals (z) </code> returns <cod     E>true</code&gt, then * <code>x.equals (z) </code> should return <code>true</code>. * <li>it is <i>consistent</i>: non-null Reference values * &LT;CODE&GT;X&LT;/CODE&GT ; and <code>y</code>, multiple invocations of * <tt>x.equals (y) </tt> consistently return &lt ;code>true</code> * or consistently return <code>false</code>, provided no * informat     Ion 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's, for any non-null reference values <code>x</code> and * &LT;CODE&GT;Y&LT;/CODE&GT;  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's generally necessary to override the <tt>hashCode</tt> * method whenever This method was overridden, so as-maintain the * general contract for the <tt>hashCode</tt> method, whic      H States * That equal objects must has equal hash codes.  *     *@paramobj The Reference object with which to compare. * @return<code>true</code> If this object is the same as the obj * argument; <code>false</c     ode> otherwise. * @see#hashCode () *@seejava.util.Hashtable*/     Public Booleanequals (Object obj) {return( This==obj); }

A pit of Java equals

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.