JDK1.6 and JDK1.7 principles and differences in substring

 

substring(int beginIndex, int endIndex)Implementation in the different versions of the JDK is different. Learn the difference between them can help you make better use of him. For simplicity, hereinafter with substring()the representative substring(int beginIndex, int endIndex)methods.

substring () action

substring(int beginIndex, int endIndex)The method of intercepting and returns the string [beginIndex, endIndex-1] in a range of content.

String x = "abcdef";
x = x.substring(1,3);
System.out.println(x);

Output content:

bc

What happens when you call substring () up?
As you probably know, because x is immutable, whenx.substring(1,3)the time of the assignment x, it will point to a new string:

However, this figure is not entirely correct representation of what happened heap. Because what happens when you call the substring in jdk6 and jdk7 are not the same.

JDK 6 in the substring

String is achieved by an array of characters. In the jdk 6, String class contains three member char value[]variables:  int offset, int count, . They are used to store real character array, the array index of the first position and the number of characters contained in the string.

When calling substring method creates a new string object, but the value of the string is still pointing to a heap of the same character array. Both objects only count and offset values ​​are different.

Here is the key to the code, said the point of view of proof in Java source code:

//JDK 6
String(int offset, int count, char value[]) {
    this.value = value;
    this.offset = offset;
    this.count = count;
}

public String substring(int beginIndex, int endIndex) {
    //check boundary
    return  new String(offset + beginIndex, endIndex - beginIndex, value);
}

JDK 6 issue of the substring caused

If you have a very, very long string, but when you use the substring you just need to be cut very short period. This may lead to performance problems because all you need is a short sequence of characters, but you have quoted the entire string (because of this very long array of characters have been cited, it can not be recovered, it could lead to a memory leak). In JDK 6, generally in the following manner to solve the problem, in fact, the principle is to generate a new string and quoted him.

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

About improper use of JDK 6 in subString cause memory series has been officially recorded in the Java Bug Database:

leak

Memory leaks: In computer science, a memory leak that due to negligence or error of the program fails to free up memory no longer in use. Memory leak does not refer to the physical memory in the disappearance, but the application after a certain period of memory allocation, due to design errors, resulting in the period prior to the release of the memory loses control of the memory segment, resulting in a waste of memory.

The JDK 7 substring

The problems mentioned above, is resolved in the jdk 7. In the jdk 7, substring method creates a new array in the heap memory.

string-substring-jdk7

Java source code for the main part about this is as follows:

//JDK 7
public String(char value[], int offset, int count) {
    //check boundary
    this.value = Arrays.copyOfRange(value, offset, offset + count);
}

public String substring(int beginIndex, int endIndex) {
    //check boundary
    int subLen = endIndex - beginIndex;
    return new String(value, beginIndex, subLen);
}

These are subString method in JDK 7, its use new Stringcreates a new string to avoid a reference to the old string. So as to solve the memory leak problem.

So, JDK version if your production environment used is less than 1.7, when you use a String subString methods must pay attention to avoid memory leaks.

Guess you like

Origin www.cnblogs.com/lujiahua/p/11408654.html