The substring() Method in JDK 6 and JDK 7
来源:互联网 发布:导入数据错误 编辑:程序博客网 时间:2024/05/16 15:28
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 followingsubstring()
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.
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 6String(int offset, int count, char value[]) {this.value = value;this.offset = offset;this.count = count;} public String substring(int beginIndex, int endIndex) {//check boundaryreturn 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 sub string:
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 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 String(value, beginIndex, subLen);}
- The substring() Method in JDK 6 and JDK 7
- The substring() Method in JDK 6 and JDK 7
- The substring() Method in JDK 6 and JDK 7
- The substring() Method in JDK 6 and JDK 7
- The substring() Method in JDK 6 and JDK 7
- The substring() Method in JDK 6 and JDK 7 (jdk6中的substring()会造成内存泄漏)
- How SubString method works in Java - Memory Leak Fixed in JDK 1.7
- GlassFish v3 requires a JDK 1.6 and not a JRE. Please add/select the correct JDK in the Server prope
- 三张图彻底了解JDK 6和JDK 7中substring的原理及区别
- Installing the Sun Java 6 JDK in Ubuntu 12.04
- The distinct between jdk and jre[转]
- Install the Oracle JDK 6
- Please make sure the -vm option in eclipse.ini is pointing to a JDK and verify that Installed JRE’s are also using JDK installs
- the relation of jre and jdk and jvm
- Please make sure the -vm option in eclipse.ini is pointing to a JDK and verify that Installed JRE’s
- Please make sure the -vm option in eclipse.ini is pointing to a JDK and verify that Installed JRE’s
- Please make sure the -vm option in eclipse.ini is pointing to a JDK and verify that Installed JRE’s
- Maven打包异常Unable to locate the Javac Compiler in:Please ensure you are using JDK 1.4 or above and
- day25_GUI
- 使用 HTML5 WebSocket 构建实时 Web 应用
- Diagram to show Java String’s Immutability
- 一年
- 如何查看sql2005数据库的端口号
- The substring() Method in JDK 6 and JDK 7
- cocos2d-x 中文显示
- Web_PHP_PHP XML SimpleXML,处理XML文件最简便方式;
- JBOSS 部署
- IP获取
- 第八章《排序技术》思维导图
- 华为机试—字符串M化成以N为单位的段
- CRF知识汇总
- NumPy学习笔记