The substring() Method in JDK 6 and JDK 7

简介: The substring() Method in JDK 6 and JDK 7  By X WangThe substring(int beginIndex, int endIndex) method in JDK 6 and JDK 7 are different.



The substring() Method in JDK 6 and JDK 7


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:

string-immutability

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.

string-substring-jdk6

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.

string-substring-jdk7

//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






目录
相关文章
|
2月前
|
Java
Java基础之 JDK8 HashMap 源码分析(中间写出与JDK7的区别)
这篇文章详细分析了Java中HashMap的源码,包括JDK8与JDK7的区别、构造函数、put和get方法的实现,以及位运算法的应用,并讨论了JDK8中的优化,如链表转红黑树的阈值和扩容机制。
36 1
JDK各个版本的特性分析|JDK7|JDK8|JDK9|JDK10|JDK11|JDK12|JDK13特性分析
JDK各个版本的特性分析|JDK7|JDK8|JDK9|JDK10|JDK11|JDK12|JDK13特性分析
|
Java 索引 存储
JDK6和JDK7中String的substring()方法及其差异
翻译人员: 铁锚 翻译日期: 2013年11月2日 原文链接: The substring() Method in JDK 6 and JDK 7    在JDK6与JDK7这两个版本中,substring(int beginIndex, int endIndex)方法是不同的.
952 0
|
SQL Java 数据库连接
JDK6笔记(5)----JDBC4
JDK6笔记(5)----JDBC4 1、Java开发者使用JDBC4 API可以做下面的事: 1) Connect to a data source 连接数据源 2) Execute complex SQL statements 执行复杂的SQL语句 ...
893 0
|
SQL Java 数据库连接
JDK6笔记(5)----JDBC4(2)
JDK6笔记(5)----JDBC4(2) 1、理解Statements 有三种类型的Statements: 1)Statements接口 它通常用于只需不带参数的SQL语句。
782 0
|
SQL Java 数据库连接
JDK6笔记(5)----JDBC4(3)
JDK6笔记(5)----JDBC4(3) 1、预准备语句的IN参数的pitfall 当你用setter方法向IN参数传递值时要注意一些问题。 不管在任何时候,设置参数并执行预准备语句对象,JDBC驱动器会把Java类型转换成DBMS所能理解的JDBC类型。
828 0
|
SQL Java 数据库连接
JDK6笔记(5)----JDBC4(4)
JDK6笔记(5)----JDBC4(4) 1、利用批处理更新 要提高性能,JDBC API提供了一个批处理更新方式,它允许你一次进行多种更新。 Statement、PreparedStatement、CallableStatement都支持批处理更新方式。
855 0
|
Java 数据库连接
JDK6笔记(5)----JDBC4(5)
JDK6笔记(5)----JDBC4(5)  1、ResultSet接口你可以指定三种常量: 1)TYPE_FORWARD_ONLY 结果集游标只能从开始到最后进行前移,它不能后退。
837 0
|
SQL Java 数据库连接
JDK6笔记(6)----JDBC4.0高级应用
JDK6笔记(6)----JDBC4.0高级应用  JDBC4.0高级应用 1、Annotations 在JDK1.5中annotation就存在了,在JDBC4中得到了加强。
848 0
|
SQL Java 数据库连接
JDK6笔记(6)----JDBC4.0高级应用(2)
JDK6笔记(6)----JDBC4.0高级应用(2) 3.4)Hibernate配置文件(Hibernate Configuration File) 在你的应用程序中典型的仅有一个hibernate.cfg.xml文件。
1209 0