Possible optimizations for the Equals () method of Java String
Last Update:2017-02-28
Source: Internet
Author: User
Optimization
The String Class code for JDK1.4 1.5 is as follows
The following is the program code
Public final class String
Implements Java.io.Serializable, Comparable<string>, charsequence
{
/** The value is used for character storage. */
Private final char value[];
/** the ' offset is ' the ' storage ' is used. */
private final int offset;
/** The count is the number of characters in the String. */
private final int count;
The following is the program code
/**
* Initializes a newly created <code>String</code> object so that it
* represents the same sequence of characters as the argument; In the other
* Words, the newly created string is a copy of the argument string. Unless
* A explicit copy of <code>original</code> is needed, with this
* constructor is unnecessary since Strings are immutable.
*
* @param original a <code>string</code>
*/
public string (string original) {
int size = Original.count;
char[] OriginalValue = Original.value;
Char[] v;
if (originalvalue.length > Size) {
The array representing the String is bigger than the new
String itself. Perhaps this constructor is being called
In order to trim the baggage and so make a copy of the array.
v = new Char[size];
System.arraycopy (OriginalValue, Original.offset, V, 0, size);
} else {
The array representing the String is the same
Size as the String, so no. making a copy.
v = originalvalue;
}
This.offset = 0;
This.count = size;
This.value = v;
}
From this constructor, we can see that it is possible to share the same char[between strings of different reference.
The following is the program code
/**
* Compares this string to the specified object.
* The result are <code>true</code> if and only if the argument isn't
* <code>null</code> and are a <code>String</code> object that represents
* The same sequence of characters as this object.
*
* @param anobject The object to compare this <code>String</code>
* against.
* @return <code>true</code> if the <code>string </code>are equal;
* <code>false</code> otherwise.
* @see Java.lang.string#compareto (java.lang.String)
* @see Java.lang.string#equalsignorecase (java.lang.String)
*/
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;
}
However, the Equals method seems to overlook this possibility. There is no direct comparison between the reference of the two char[].
According to my idea, should join this paragraph.
The following is the program code
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;
////{{
if (i = = J && V1 = = v2) return true; Note:this line are added by me
////}}
while (n--! = 0) {
if (v1[i++]!= v2[j++])
return false;
}
This will be able to correspond to share char[] situation, can speed up the comparison speed.