Possible optimizations for the Equals () method of Java String

Source: Internet
Author: User
Tags array object comparison constructor copy final int size string
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&gt, 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.














Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.