how does String.equals() work

后端 未结 2 1687
粉色の甜心
粉色の甜心 2021-01-20 05:24

I have been trying to understand how some of API methods work

below is a snippet of equals method of java.lang.String class

Can someone out there tell me how

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-20 05:44

    Logically

    while (n-- != 0) {
    if (v1[i++] != v2[j++])
        return false;
    }
    

    is the same as

    for (int i = 0; i < n; i++) {
        if (v1[i] != v2[j])
            return false;
        }
    }
    

    Why the JVM designers have done it this way I am not sure. Perhaps there is a performance improvement using a while loop than a for loop. It looks quite C like to me so maybe the person who wrote this has a background in c.

    Offset is used to locate where the string starts within the char array. Internally Strings are stored as char arrays. This is value

    if (v1[i++] != v2[j++])
        return false;
    

    checks the characters in the string's underlying char array.

    and line by line it is

    if the refernce is pointing to the same object is must the equals

    1014           if (this == anObject) {
    1015               return true;
    1016           }
    

    if the object is a string then check they are equal

    1017           if (anObject instanceof String) {
    

    cast the parameter passed in as String.

    1018               String anotherString = (String)anObject;
    

    remember the length of this.string

    1019               int n = count;
    

    if the two string's lengths match

    1020               if (n == anotherString.count) {
    

    get an array of the characters (value is this array)

    1021                   char v1[] = value;
    1022                   char v2[] = anotherString.value;
    

    find out where in this array the string starts

    1023                   int i = offset;
    1024                   int j = anotherString.offset;
    

    loop through char array. if the values are different then return false

    1025                   while (n-- != 0) {
    1026                       if (v1[i++] != v2[j++])
    1027                           return false;
    1028                   }
    

    everything else must be true

    1029                   return true;
    1030               }
    1031           }
    

    if not of type String then they cannot be equals

    1032           return false;
    1033       }
    

    To understand offset and value look at the String class

    /** The value is used for character storage. */
    private final char value[];
    
    /** The offset is the first index of the storage that is used. */
    private final int offset;
    
    /** The count is the number of characters in the String. */
    private final int count;
    

    The constructors initialises these variables. The default constructor code is below. You should see something similar for the other constructors.

    /**
      * Initializes a newly created {@code String} object so that it represents
      * an empty character sequence.  Note that use of this constructor is
      * unnecessary since Strings are immutable.
      */
     public String() {
        this.offset = 0;
        this.count = 0;
        this.value = new char[0];
     }
    

    This is quite a good link to look at

提交回复
热议问题