The substring() Method in JDK 6 and JDK 7

The substring() Method in JDK 6 and JDK 7

  By X Wang

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 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 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 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);
}

Top 10 questions about Java String.
References:
1. Changes to substring
2. Java 6 vs Java 7 when implementation matters

时间: 2024-07-29 22:54:28

The substring() Method in JDK 6 and JDK 7的相关文章

centos 7 安装JDK (Linux安装jdk)

centos 7安装JDK (Linux安装jdk) 第一部分 首先查看centos 7是否有openjdk,如没有就跳过第一部分,直接第二部分. [master@bogon ~]$ java -version openjdk version "1.8.0_65" OpenJDK Runtime Environment (build 1.8.0_65-b17) OpenJDK 64-Bit Server VM (build 25.65-b01, mixed mode) 1.0 查询包含

jdk环境配置问题-jdk的安装以及环境的配置

问题描述 jdk的安装以及环境的配置 安装jdk配置环境以后,在cmd中测试,javac可以通过,为什么java就通不过去呢 解决方案 装jdk的时候java虚拟机是不是有问题了? 解决方案二: 应该是环境变量的问题,看看你的path和classpath是否是正确的,最可能的错误是classpath中少了前面少了.;建议楼主贴一下环境变量 解决方案三: http://www.cnblogs.com/zhj5chengfeng/archive/2013/01/01/2841253.html对照这篇

jdk java-win7打开jdk文档出现错误

问题描述 win7打开jdk文档出现错误 解决方案 在运行中分别输入这四个运行就行了: regsvr32 jscript.dll regsvr32 hhctrl.ocx regsvr32 itss.dll regsvr32 itircl.dll 每运行一个都会弹一个窗口说XXXXXX启动成功. 解决方案二: 重新下载一个吧,网的JDK API 多的是. 解决方案三: 应该是文档内部的错误,可以选择重新下载一个

android jdk环境配置-JDK环境变量配置???

问题描述 JDK环境变量配置??? android的JDK怎么配置?还有我以前弄JAVA配置过JDK,现在需要把他重新配置吗?求解!!!

作spring , j2ee的话,open jdk和oracle jdk 有区别么?推荐

问题描述 作spring,j2ee的话,openjdk和oraclejdk有区别么?推荐哪个? 解决方案 解决方案二:openjdk是sun2006年开放的jdk的源码项目oraclejdk采用了商业实现,包含一些openjdk没有的商业闭源功能普通开发应用,用oraclejdk就可以了如果研究jvm,想通过改写源码提高jdk性能的话,那就拿openjdk开刀吧.

linux下查看已经安装的jdk 并卸载jdk的方法(推荐)_Linux

一.查看Jdk的安装路径: whereis java which java (java执行路径) echo $JAVA_HOME echo $PATH 备注:如果是windows中,可以使用: set java_home:查看JDK安装路径 java -version:查看JDK版本 二.卸载旧版本的JDK: 1. 确定JDK的版本: rpm -qa | grep jdk rpm -qa | grep gcj 可能的结果是: libgcj-4.1.2-42.el5 java-1.4.2-gcj-

…… are only available on JDK 1.5 and higher 错误(spring 的jdk版本检测在jdk 8下的修订)

项目中用的是spring低版本(2.5.6),今天用jre 8测试了一下,发现错误: Unexpected exception parsing XML document from class path resource [applicationContext-dao.xml]; nested exception is java.lang.IllegalStateException: Context namespace element 'annotation-config' and its par

【Linux】CentOS7上安装JDK 和卸载 JDK 【rpm命令的使用】

之前有过一篇在CentOS7上安装JDK的文章:http://www.cnblogs.com/sxdcgaq8080/p/7492426.html   在这里又说一次,是要使用rpm命令安装JDK的rpm包,和卸载已经安装的JDK 1.在官网下载JDK的rpm安装包 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 这里还使用了CentOS7上自带的下载工具,下载速度可以快一点,

【JDK和Open JDK】平常使用的JDK和Open JDK有什么区别

注意到这个问题,是在CentOS7上安装JDK的时候,查找相关的资料,发现安装JDK之前都需要检查或卸载系统上原生的Open JDK,这才引起了注意. 到了这里,引用查到的一篇说明. 转自:http://fgh2011.iteye.com/blog/1771649   历史上的原因是,openjdk是jdk的开放原始码版本,以GPL协议的形式放出.在JDK7的时候,openjdk已经成为jdk7的主干开 发,sun jdk7是在openjdk7的基础上发布的,其大部分原始码都相同,只有少部分原始