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 * <CODE>TRUE</CODE>. * <li>it is <i>symmetric</i>: 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> 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>, then * <code>x.equals (z) </code> should return <code>true</code>. * <li>it is <i>consistent</i>: 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 * 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 * <CODE>Y</CODE> 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