Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how does String.equals() work

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 actually the code is comparing two strings. I get the significance of count, but what does offset signify. how are these variables getting values ?

Like when i create a String. how are these initialized.

a detailed line by line description and also how and when the instance variables, value, count, offset etc are initialized ??

 public boolean equals(Object anObject) {
  1014           if (this == anObject) {
  1015               return true;
  1016           }
  1017           if (anObject instanceof String) {
  1018               String anotherString = (String)anObject;
  1019               int n = count;
  1020               if (n == anotherString.count) {
  1021                   char v1[] = value;
  1022                   char v2[] = anotherString.value;
  1023                   int i = offset;
  1024                   int j = anotherString.offset;
  1025                   while (n-- != 0) {
  1026                       if (v1[i++] != v2[j++])
  1027                           return false;
  1028                   }
  1029                   return true;
  1030               }
  1031           }
  1032           return false;
  1033       }
like image 923
Mukul Goel Avatar asked Jul 07 '26 03:07

Mukul Goel


1 Answers

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

like image 194
RNJ Avatar answered Jul 10 '26 09:07

RNJ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!