What is the difference between a Java string and an android string?

Source: Internet
Author: User

This is today Ali telephone interview was asked, before really did not think (always thought is the same), so after the interview, I immediately opened the source code, the two string classes are compared, the following is my discovery. First I looked at the packages imported by these two string classes and found some differences:

This is the Android version:

Import Java.io.serializable;import Java.io.unsupportedencodingexception;import Java.nio.bytebuffer;import Java.nio.charbuffer;import Java.nio.charset.charset;import Java.nio.charset.charsets;import java.util.Arrays; Import Java.util.comparator;import java.util.formatter;import java.util.locale;import Java.util.regex.Pattern; Import libcore.util.emptyarray;//This seems to be Google's own API class library

This is the Java version:

Import Java.io.objectstreamfield;import Java.io.unsupportedencodingexception;import java.nio.charset.Charset; Import Java.util.arraylist;import java.util.arrays;import Java.util.comparator;import Java.util.Formatter;import Java.util.locale;import Java.util.regex.matcher;import Java.util.regex.pattern;import Java.util.regex.PatternSyntaxException;

You can see the Android version of the more than a few NIO package inside the class, visual Android on the performance of the string class is optimized. Next, I found a difference in the Comparetoignorecase method for two classes: Android version:
public int Comparetoignorecase (string string) {        int o1 = offset, O2 = String.offset, result;        int end = offset + (count < String.count count:string.count);        Char C1, C2;        char[] target = string.value;        while (O1 < end) {            if ((c1 = value[o1++]) = = (C2 = target[o2++])) {                continue;            }            C1 = Foldcase (c1);            C2 = foldcase (C2);            if (result = c1-c2)! = 0) {                return result;            }        }        return count-string.count;    }

Java version:

  public int comparetoignorecase (String str) {        return case_insensitive_order.compare (this, str);    }

Look carefully, originally just the reverse order, essentially the same, Android will logically write to the comparetoignorecase, The Compare method in the Caseinsensitivecomparator comparator invokes the opposite of Comparetoignorecase,java version. The next interesting thing, I found that many of the methods of Android have become local methods:
Public native char charAt (int index);  public native int CompareTo (string string);p ublic native Boolean equals (Object object);p rivate native int fastindexof (int c, int start);p ublic native Boolean isEmpty ();p ublic native int length ();

and these methods are not native in Java:

public char charAt (int index) {if (Index < 0) | | (index >= value.length))        {throw new stringindexoutofboundsexception (index);    } return Value[index];        } public int CompareTo (String anotherstring) {int len1 = value.length;        int len2 = AnotherString.value.length;        int lim = Math.min (len1, len2);        Char v1[] = value;        Char v2[] = Anotherstring.value;        int k = 0;            while (K < Lim) {char C1 = v1[k];            char C2 = v2[k];            if (c1! = C2) {return c1-c2;        } k++;    } return len1-len2;        } 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; }

As you can see, Android is really optimized for common APIs by using local methods, which is more efficient than Java version estimates. In addition, I found that many of the methods of the Android version are annotated, such as @findbugssuppresswarnings, @SuppressWarnings, and so on, while the Java version does not.
Limited by personal level, can only be analyzed here ~




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.