The substring() Method in JDK 6 and JDK 7
2016-04-01 00:00
183 查看
摘要: The substring(int beginIndex, int endIndex) method in JDK 6 and JDK 7 are different. Knowing the difference can help you better use them. For simplicity reasons, in the following substring() represent the substring(int beginIndex, int endIndex) method.
1. What substring() does?
The substring(int beginIndex, int endIndex) method returns a string that starts with beginIndex and ends with endIndex-1.
Output: bc
2. What happens when substring() is called?
You may know that because x is immutable, when x is assigned with the result of x.substring(1,3), it points to a totally new string like the following:
However, this diagram is not exactly right or it represents what really happens in the heap. What really happens when substring() is called is different between JDK 6 and JDK 7.
3. substring() in JDK 6
String is supported by a char array. In JDK 6, the String class contains 3 fields: char value[], int offset, int count. They are used to store real character array, the first index of the array, the number of characters in the String.
When the substring() method is called, it creates a new string, but the string's value still points to the same array in the heap. The difference between the two Strings is their count and offset values.
The following code is simplified and only contains the key point for explain this problem.
4. A problem caused by substring() in JDK 6
If you have a VERY long string, but you only need a small part each time by using substring(). This will cause a performance problem since you need only a small part, you keep the whole thing. For JDK 6, the solution is using the following, which will make it point to a real substring:
5. substring() in JDK 7
This is improved in JDK 7. In JDK 7, the substring() method actually create a new array in the heap.
1. What substring() does?
The substring(int beginIndex, int endIndex) method returns a string that starts with beginIndex and ends with endIndex-1.
String x = "abcdef"; x = x.substring(1,3); System.out.println(x);
Output: bc
2. What happens when substring() is called?
You may know that because x is immutable, when x is assigned with the result of x.substring(1,3), it points to a totally new string like the following:
However, this diagram is not exactly right or it represents what really happens in the heap. What really happens when substring() is called is different between JDK 6 and JDK 7.
3. substring() in JDK 6
String is supported by a char array. In JDK 6, the String class contains 3 fields: char value[], int offset, int count. They are used to store real character array, the first index of the array, the number of characters in the String.
When the substring() method is called, it creates a new string, but the string's value still points to the same array in the heap. The difference between the two Strings is their count and offset values.
The following code is simplified and only contains the key point for explain this problem.
//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); }
4. A problem caused by substring() in JDK 6
If you have a VERY long string, but you only need a small part each time by using substring(). This will cause a performance problem since you need only a small part, you keep the whole thing. For JDK 6, the solution is using the following, which will make it point to a real substring:
x = x.substring(x, y) + ""
5. substring() in JDK 7
This is improved in JDK 7. In JDK 7, the substring() method actually create a new array in the heap.
//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); }
相关文章推荐
- javascript Split方法,indexOf方法、lastIndexOf 方法和substring 方法
- 常常会用到的截取字符串substr()、substring()、slice()方法详解
- JS中的substring和substr函数的区别说明
- js substr、substring和slice使用说明小记
- js中substring和substr的详细介绍与用法
- javascript indexOf方法、lastIndexOf 方法和substring 方法
- javascript中String类的subString()方法和slice()方法
- sqlserver substring函数使用方法小结
- JavaScript中使用Substring删除字符串最后一个字符
- JavaScript截取字符串的Slice、Substring、Substr函数详解和比较
- 深入浅析JavaScript字符串操作方法 slice、substr、substring及其IE兼容性
- 3种js实现string的substring方法
- 浅谈Javascript中substr和substring的区别
- mysql截取的字符串函数substring_index的用法
- js中substr,substring,indexOf,lastIndexOf,split,replace的用法详解
- js substring()字符串截取函数
- Js中的substring,substr与C#中的Substring比较
- js中substring和substr的定义和用法
- JavaScript字符串对象substring方法入门实例(用于截取字符串)
- js中substr,substring,indexOf,lastIndexOf的用法小结