Guest
Guest

Reputation: 515

Explain the overridden method of equal in String class

In String class overridden method equal use the count, value and offset. What are they, Why we are not using very simple like for count, we can use length() function, value which is array we can use toCharArray(); and for offset we can take length()-1. I tried to search those keywords count, value and offset in Java Documentation but not found....

public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    if (anObject instanceof String) {
        String anotherString = (String)anObject;
        int n = count;
        if (n == anotherString.count) {
        char v1[] = value;
        char v2[] = anotherString.value;
        int i = offset;
        int j = anotherString.offset;
        while (n-- != 0) {
            if (v1[i++] != v2[j++])
            return false;
        }
        return true;
        }
    }
    return false;
    }

Upvotes: 0

Views: 101

Answers (3)

Davide Lorenzo MARINO
Davide Lorenzo MARINO

Reputation: 26926

Here are the definitions:

value  -> array of char holding the chars present in the String
count  -> number of chars in the String
offset -> offset of first character to be considered in the value array

For example, consider the array

char[] chars = new char[]{'a', 'b', 'c', 'd', 'e'};
String str = new String(chars, 1, 2);
System.out.println(str);   // Prints bc  

char[] chars2 = new char[]{'b', 'c'};
String str2 = new String(chars2, 0, 2);
System.out.println(str2);   // Prints bc

System.out.println(str.equals(str2));  // Prints true  

You can imagine that value is ['a', 'b', 'c', 'd', 'e'] for the String str and ['b', 'c'] for the String str2.

This is not true. Both strings internally use an array of chars of size 2, the array ['b', 'c'].

But when you ask for a substring it creates a new String, with the same value and different values of offset and count.


Here a description of value, offset, count with some example

command                             value           count  offset   toString
---------------------------------------------------------------------------
String str = new String("ABC");     ['A', 'B', 'C']   3      0       ABC
str.substring(2);                   ['A', 'B', 'C']   1      2       C    
str.substring(1, 2);                ['A', 'B', 'C']   2      1       BC

Upvotes: 3

Stefan
Stefan

Reputation: 444

The offset and count fields are used, because the String compared may be a substring of another String. In this case no new String object is created but the string points to a location within another String. Thats why the count and offset fields exist.

Example:

String s1 = "DoTheHuzzle";
String s2 = s1.subString(2, 5);   // "The"

In this case s2 has an offset of 2 and a length of 3.

Upvotes: 0

Naman
Naman

Reputation: 31968

From JDK 1.8.0_65 -> java.lang package -> String.java class :

/**
 * Compares this string to the specified object.  The result is {@code
 * true} if and only if the argument is not {@code null} and is a {@code
 * String} object that represents the same sequence of characters as this
 * object.
 *
 * @param  anObject
 *         The object to compare this {@code String} against
 *
 * @return  {@code true} if the given object represents a {@code String}
 *          equivalent to this string, {@code false} otherwise
 *
 * @see  #compareTo(String)
 * @see  #equalsIgnoreCase(String)
 */
public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    if (anObject instanceof String) {
        String anotherString = (String)anObject;
        int n = value.length;
        if (n == anotherString.value.length) {
            char v1[] = value;
            char v2[] = anotherString.value;
            int i = 0;
            while (n-- != 0) {
                if (v1[i] != v2[i])
                    return false;
                i++;
            }
            return true;
        }
    }
    return false;
}

where value is defined as

/** The value is used for character storage. */
private final char value[];

So I hope that pretty much answers your doubt.

Upvotes: 1

Related Questions