The similarities and differences between equals () & = = In Java can be obtained by flipping through the JDK source Java.lang.String.equals (Object anobject) to obtain a perceptual knowledge:
1 /**2 * Compares this string to the specified object. The result is {@code3 * true} if and only if the argument are not {@codenull} and is a {@code4 * String} object that represents the same sequence of characters as this5 * object.6 *7 * @paramAnObject8 * The object to compare this {@codeString} against9 *Ten * @return {@codetrue} If the given object represents a {@codeString} One * Equivalent to this string, {@codefalse} otherwise A * - * @see#compareTo (String) - * @see#equalsIgnoreCase (String) the */ - Public Booleanequals (Object anobject) { - if( This==anobject) { - return true ; + } - if(AnObjectinstanceofString) { +String anotherstring =(String) anobject; A intn =count; at if(n = =anotherstring.count) { - CharV1[] =value; - CharV2[] =Anotherstring.value; - inti =offset; - intj =Anotherstring.offset; - while(n--! = 0) { in if(v1[i++]! = v2[j++]) - return false ; to } + return true ; - } the } * return false; $}
From the above you can see the case where the java.lang.String.equals (Object AnObject) method returns true:
- Compare yourself with yourself;
- Two objects are string and the contents are the same.
To be aware of:
- Java does not support operator overloading (Operator overloading);
- In Java, Equals () and hashcode () are closely related, and the two methods are rewritten (overriding), so the specific meaning of equals () depends on the code implementation.
Java equals () & = =