SUBSTRING () method differs in JDK6 and JDK7 (translation of foreign blogs)

Source: Internet
Author: User

The substring (int beginindex, int endIndex) method is not the same in JDK6 and JDK7, and understanding the two versions that make it different will allow you to use them better.

1. What is the role of substring ()?

The substring (int beginindex, int endIndex) method returns a starting position of beginindex (inclusive), and the end position is a string of endIndex-1 (inclusive). For example:

Input:

String x = "ABCdef"; x = x.substring (1,3); SYSTEM.OUT.PRINTLN (x);

Output:

Bc

2. What happens when the substring () method is called?

You may know that X is immutable, and when x points to the result of x.substring (1,3), it actually points to a completely new string as follows:

However, this picture is not accurate or it shows what really happened inside the heap. What difference does JDK6 and JDK7 really make when the substring () method is called?

3, substring () in JDK6

A String is represented by a character array.  The Jdk6,string class contains 3 fields: Char value[],int offset,int count. They are used to store a true array of letters, the first coordinate of an array, and the number of letters in a string.

When substring () is called, it creates a new string, but the value of the string in the heap still points to the same array. The two strings differ only by the value of count and offset.

The following code simplifies and is critical to explaining this problem

JDK 6String (int offset, int count, Char value[]) {this.value = Value;this.offset = Offset;this.count = count;} public St Ring substring (int beginindex, int endIndex) {//check boundaryreturn new String (offset + beginindex, Endindex-begininde x, value);}

4. The problem of substring () in JDK6

This can cause performance problems if you have a long string, but you only need a small fraction of each call to substring (). Because you only need a part, but keep the whole space. For JDK6, here's a workaround, which can point to a true substring:

x = x.substring (x, y) + ""

5, in the JDK7 of the substring ()

Improved in JDK7, in JDK7, the substring () method really creates a new array in the heap

JDK 7public String (char value[], int offset, int count) {//check Boundarythis.value = arrays.copyofrange (value, offset, Offset + count);} Public String substring (int beginindex, int endIndex) {//check Boundaryint Sublen = Endindex-beginindex;return new Strin G (Value, Beginindex, Sublen);}

(Translated, original link: http://www.programcreek.com/2013/09/the-substring-method-in-jdk-6-and-jdk-7/)

For the 4th, personally add:

x = x.substring (x, y) + ""

The above code implementation process is as follows:

StringBuilder sb = new StringBuilder () Sb.append (x.substring (x, y)); Sb.append (""); x = Sb.tostring ();

Use the following method instead:

x = new String (x.substring (x, y));

This saves an object reference and a little bit of time.

Personal views, if there is a problem, welcome message exchange ~








SUBSTRING () method differs in JDK6 and JDK7 (translation of foreign blogs)

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.