打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
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 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.

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

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
简易Java(07):substring()方法在JDK6和JDK7中的异同 | "地瓜哥"博客网
Java的String中的subString()方法
Java中的substring真的会引起内存泄露么?
java用substring函数截取string中一段字符串
谈谈String和StringBuffer
Java 性能优化之 String 篇
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服