参考:http://www.programcreek.com/2013/09/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:

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

Top 10 questions about Java String.

References:
1. Changes to substring
2. Java 6 vs Java 7 when implementation matters

The substring() Method in JDK 6 and JDK 7 (jdk6中的substring()会造成内存泄漏)相关推荐

  1. JDK6和JDK7中的substring()方法

    2019独角兽企业重金招聘Python工程师标准>>> substring(int beginIndex, int endIndex)在JDK6与JDK7中的实现方式不一样,理解他们 ...

  2. Java中由substring方法引发的内存泄漏

    欢迎支持笔者新作:<深入理解Kafka:核心设计与实践原理>和<RabbitMQ实战指南>,同时欢迎关注笔者的微信公众号:朱小厮的博客. 在Java中我们无须关心内存的释放,J ...

  3. The substring() Method in JDK 6 and JDK 7

    2019独角兽企业重金招聘Python工程师标准>>> 1. What substring() does? The substring(int beginIndex, int end ...

  4. 在JDK 6和JDK 7的substring()方法的区别?

    原文链接:https://www.programcreek.com/2013/09/the-substring-method-in-jdk-6-and-jdk-7/ 在JDK 6和JDK 7中subs ...

  5. ubuntu安装jdk语句_JDK 12:实际中的切换语句/表达式

    ubuntu安装jdk语句 我的上一篇文章" 玩JDK 12的Switch表达式 "讨论了使用JDK 12 Early Access Builds尝试JEP 325 switch ...

  6. sun jdk 与jdk_Sun过去的世界中的JDK 11和代理

    sun jdk 与jdk 使用JDK 11后,就sun.misc.Unsafe的第一种方法. 其中, defineClass方法已删除. 代码生成框架通常使用此方法在现有的类加载器中定义新的类. 尽管 ...

  7. JDK 7和JDK 8中大行读取速度较慢的原因

    我之前发布了博客文章"使用JDK 7和JDK 8读取慢速行",并且在该问题上有一些有用的评论来描述该问题. 这篇文章提供了更多解释,说明为何该文章中演示的文件读取(并由Ant的Li ...

  8. 一文汇总 JDK 5 到 JDK 15 中的牛逼功能!

    前言 JDK 16 马上就要发布啦(预计 2021.3.16 日发布),所以在发布之前,让我们先来回顾一下 JDK 5-15 的新特性吧,大家一起学起来~ Java 5 新特性 1. 泛型 泛型本质是 ...

  9. 关于使用JAVA中JDK安装和在命令行中编译和运行程序的一些总结

    JDK安装:http://www.oracle.com/technetwork/java(可以选择安装路径) (在写下载这个JDK时需要先创建账户) 1.下载完之后就可以安装了,安装路径自己可以设置, ...

最新文章

  1. Same binary weight (位运算)
  2. .net的mvc的fw版本为4.5发布到阿里云【云虚拟主机】上.
  3. 利润中心, 成本中心, 工作中心
  4. python编程内置函数使用方法_python编程(4)--内置函数
  5. sqlserver sa
  6. 264 解码之 yuv
  7. 区块链开发公司 注重用户的价值才是企业归宿
  8. Spring Boot 2 快速教程:WebFlux 集成 Thymeleaf 、 Mongodb 实践(六)
  9. Selenium UI自动化测试(二)IDE—使用
  10. zynq开发系列1:vivado和sdk联合使用串口打印helloworld
  11. 【Mybatis】Mybatis三大组件之StatementHandler
  12. Windows系统镜像下载
  13. 计算机课后感400字,观后感400字
  14. 基于java中国跳棋游戏
  15. Git设置代理服务器
  16. 强烈推荐这 15 个网站!
  17. [ 系统安全篇 ] 拉黑IP - 火绒安全软件设置IP黑名单 windows使用系统防火墙功能设置IP黑名单
  18. 计算机网络实验报告一
  19. Windows API一日一练 29 SelectObject和DeleteObject函数
  20. MA模型自协方差证明

热门文章

  1. beangle commons 4.0.0 release
  2. windows网络负载平衡
  3. [转]Windows CE 编程的十点忠告(by pdasky)
  4. ONAP — 边缘自动化方案
  5. Segment Routing — SRv6 — Overview
  6. 用 C 语言开发一门编程语言 — 基于 Lambda 表达式的函数设计
  7. 手动部署 Ceph Mimic 三节点
  8. Python_List对象内置方法详解
  9. ARM启动代码中_main 与用户主程序main()的区别
  10. Countly 19.02.1 发布,实时移动和 web 分析报告平台