What is the tring difference between Javas Tring and androids?

Source: Internet
Author: User
Tags locale

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. Find some differences:

This is the Android version number:

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 number:

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;

Can see the Android version of the more than a few NIO package inside the class. Android has optimized the performance of the String class. Next. I found a difference in the Comparetoignorecase method for two classes: Android version number:
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 number:

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

A careful look, the original is only the reverse of the order, essentially the same. Android writes logic to the comparetoignorecase. And the Compare method in the Caseinsensitivecomparator is called the Comparetoignorecase,java version of the contrary.

The next is more interesting. I've found that very many 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; }

Visible. Android is really optimizing the APIs that are used on a local basis, which is more efficient than the Java version number. In addition, I found that the Android version number of the very many methods are added to the annotations. such as @findbugssuppresswarnings, @SuppressWarnings and so on. And the Java version number does not.


Limited by personal level, only can analyze to this point ~




Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

What is the tring difference between Javas Tring and androids?

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.