Different implementation of String. substring in Android and Java, androidsubstring

Source: Internet
Author: User

Different implementation of String. substring in Android and Java, androidsubstring

I am lucky to go to sogou Ba today. I have a very interesting question.

String str1 = "test for sougou";String str2 = str1.substring(5);

The test site is whether str2 generates a new character array to save "for sougou"


At that time, I thought that the String contains a char [], which cannot be reused by adding a number to the first address like cpp.

The new string must be ArrayCopy once to implement the substring function. Therefore, a new memory must be generated.


Let's take a look at the implementation.

Because android studio is open, I read the implementation of String. substring in android and sent it to my classmates.

public String substring(int start) {        if (start == 0) {            return this;        }        if (start >= 0 && start <= count) {            return new String(offset + start, count - start, value);        }        throw indexAndLength(start);    }
String(int offset, int charCount, char[] chars) {        this.value = chars;        this.offset = offset;        this.count = charCount;    }

The member variable offset is used to save the lower offset and char [] is directly referenced to a new String without applying for memory.

While sighing the subtle implementation, I found myself doing wrong



Who knows what version of jdk I use after reading the source code? The implementation is different from that on the other side?

public String substring(int beginIndex) {        if (beginIndex < 0) {            throw new StringIndexOutOfBoundsException(beginIndex);        }        int subLen = value.length - beginIndex;        if (subLen < 0) {            throw new StringIndexOutOfBoundsException(subLen);        }        return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);    }

public String(char value[], int offset, int count) {        if (offset < 0) {            throw new StringIndexOutOfBoundsException(offset);        }        if (count < 0) {            throw new StringIndexOutOfBoundsException(count);        }        // Note: offset or count might be near -1>>>1.        if (offset > value.length - count) {            throw new StringIndexOutOfBoundsException(offset + count);        }        this.value = Arrays.copyOfRange(value, offset, offset+count);    }
Except that beginIndex = 0 is used to directly return the current data, ArrayCopy is used for other operations.


Yes !!! New memory is added in JDK !!!
I have to say that google programmers are indeed superior.




Java code (java in android): public String substring (int start) {if (start = 0) {return this ;}

String (int offset, int count, char [] value) is a non-public constructor of String! You cannot find it in the API documentation. You can only find it by looking at the implementation of the underlying source code. In fact, this constructor internally implements the String. substring method by adjusting the offset value and the valid length value (count) of the value! The exact point is that the String object generated by the substring method shares a value with the original String object, instead of copying the char [] substring!

What is the difference between a view written by xlm during Android development and a view written by java?

XMl is simpler, but in fact, Android still needs to parse your layout XML code before you can draw a View. While the java code writing layout is a little complex, it is more straightforward. To make a control with better encapsulation, use the code layout. Code layout is not difficult, for example, defining a LinearLayout object, and then adding a subview through its addView method, which has a parameter LayoutParam, used to set the width, height, margin, gravity, padding, weight, and so on of the sub-spaces.

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.