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 }
Logically
is the same as
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 isvalue
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
if the object is a string then check they are equal
cast the parameter passed in as String.
remember the length of this.string
if the two string's lengths match
get an array of the characters (value is this array)
find out where in this array the string starts
loop through char array. if the values are different then return false
everything else must be true
if not of type String then they cannot be equals
To understand offset and value look at the String class
The constructors initialises these variables. The default constructor code is below. You should see something similar for the other constructors.
This is quite a good link to look at
As you may be knowing that string handling in Java is a special case, most of the time the String is assigned from the String pools, so it might be the case that for a char array
"I am Learning Java"
, one string reference points to"I am Learning Java"
, then offset would be0
, other string might point to"am"
so offset would be2
. As some of the native code handles its initilization so i think offset is set during that process.(during sharing memory from String pool)Also as you can see from the code
When new String is created from the old one, it might be the case that old string(original in this case) might be from String pool, thats why first a offset is taken and then the whole array is copied to allocate new memory(new string doesn't share memory from String pool)
Also you should remember String is a derived type and the string is always stored in a character array, so we need an offset to determine from where the string starts in the character array.