equals

equals method:

First look at the definition of the equals method:

 

From the above we can see that the equals method in the String class is an overwrite of the equals method in the parent class Object class. Let's first look at how the equals method of the Object class is written:

[java]  view plain copy  
 
  1. @param   obj   the reference object with which to compare.  
  2.     * @return  {@code true} if this object is the same as the obj  
  3.     *          argument; {@code false} otherwise.  
  4.     * @see     #hashCode()  
  5.     *  see java.util.HashMap  
  6.     */  
  7.    public boolean equals(Object obj) {  
  8.        return (this == obj);  
  9.    }  

That is, directly return (this==obj) where an object calls the method, and this refers to that object.

 

Let's take a look at how the equals method in the String class overrides the above methods:

[java]  view plain copy  
 
  1. /** 
  2.      * Compares this string to the specified object.  The result is {@code 
  3.      * true} if and only if the argument is not {@code null} and is a {@code 
  4.      * String} object that represents the same sequence of characters as this 
  5.      * object. 
  6.      * 
  7.      * @param  anObject 
  8.      *         The object to compare this {@code String} against 
  9.      * 
  10.      * @return  {@code true} if the given object represents a {@code String} 
  11.      *          equivalent to this string, {@code false} otherwise 
  12.      * 
  13.      * @see  #compareTo(String) 
  14.      * @see  #equalsIgnoreCase(String) 
  15.      */  
  16.     public boolean equals(Object anObject) {  
  17.         if (this == anObject) {  
  18.             return true;  
  19.         }  
  20.         if (anObject instanceof String) {  
  21.             String anotherString = (String) anObject;  
  22.             int n = value.length;  
  23.             if (n == anotherString.value.length) {  
  24.                 char v1[] = value;  
  25.                 char v2[] = anotherString.value;  
  26.                 int i = 0;  
  27.                 while (n-- != 0) {  
  28.                     if (v1[i] != v2[i])  
  29.                             return false;  
  30.                     i++;  
  31.                 }  
  32.                 return true;  
  33.             }  
  34.         }  
  35.         return false;  
  36.     }  


As you can see, there is an array of values ​​in this equals method. Find its definition:

It can be seen that value is an array of char type, which is used to store each character in the string. It is consistent with the definition of the String class above (nonsense, it is not).

 Analyzing conditions:

    Returns true if the current object and the compared object are the same object. That is, the equals method in Object.

    If the current incoming object is of type String, compare the lengths of the two strings, that is, the length of value.length.

          If the lengths are not the same, return false

          If the lengths are the same, compare each bit in the array value, if they are different, return false. Returns true if every bit is the same.

    If the currently passed in object is not of type String, return false directly

   

 

************************************************ The change of the equals method of the String class Body Form *****************************************

Require:

Write a class MyString, which has a char[ ] value, implement the equalsString method inside, and require that two objects of the MyString class can be compared. Returns 0 for equality, 1 for greater than, -1 for less than, and -100 if the comparison is not an object of type MyString.

 

code show as below:

[java]  view plain copy  
 
  1. package cn.ywq.test;  
  2.   
  3. class MyString {  
  4.       
  5.     char[] value;  
  6.     public MyString(char[] value) {  
  7.         this.value=value;   //Pass in the character through the constructor  
  8.     }  
  9.       
  10.       
  11.     public int equalsString(Object obj) {  
  12.         if(this==obj){  
  13.             return 0;  
  14.         }  
  15.           
  16.         //If the object is of type MyString  
  17.         if(obj instanceof MyString){  
  18.             MyString string =(MyString) obj;  
  19.             int n=this.value.length;  
  20.             if (n>string.value.length) {   //First determine the relationship of length  
  21.                 return 1;  
  22.             }else if(n<string.value.length){  
  23.                 return -1;  
  24.             } else{    //if the length is equal  
  25.                  char v1[] = this.value;  
  26.                  char v2[] = string.value;  
  27.                     int i = 0;  
  28.                     while (n-- !=  0) {   //compare each bit of the array  
  29.                         if (v1[i] > v2[i]){  
  30.                             return 1;  
  31.                         }else if(v1[i] < v2[i]){  
  32.                             return -1;  
  33.                         }  
  34.                                   
  35.                         i++;  
  36.                     }  
  37.                     return  0;   //If the while loop ends normally, it means equal, return 0  
  38.   
  39.             }  
  40.         }  
  41.         return - 100;   //If the incoming object is not of type MyString  
  42.           
  43.     }  
  44.   
  45. }  

 

Test code:

[java]  view plain copy  
 
    1. package cn.ywq.test;  
    2.   
    3. public class Test {  
    4.   
    5.     public static void main(String[] args) {  
    6.           
    7.         char[] value={'a','b','c','d'};  
    8. //      char[] value2={'a','b','c','d','e'};  
    9. //      char[] value3={'c','b','c','d'};  
    10.         char[] value4={'a','b','c',};  
    11.           
    12.         MyString myString = new MyString(value);  
    13.         MyString s=new MyString(value4);  
    14.         int i = myString.equalsString(s);   
    15.         System.out.println(i);  
    16.     }  
    17.   
    18. }  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325140678&siteId=291194637