2019独角兽企业重金招聘Python工程师标准>>>

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 substring:

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

转载于:https://my.oschina.net/wangbaofeng/blog/651432

The substring() Method in JDK 6 and JDK 7相关推荐

  1. The substring() Method in JDK 6 and JDK 7 (jdk6中的substring()会造成内存泄漏)

    参考:http://www.programcreek.com/2013/09/the-substring-method-in-jdk-6-and-jdk-7/ The substring(int be ...

  2. 在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 ...

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

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

  4. IBM JDK,SUN JDK,BEA JDK区别在哪里?

    这是java.sun.com网站上的一张关于JDK构成的图片说明.其中每一个方框(例如AWT,String,IDL,JDBC...)表示JDK的一个"技术领域". 不知道SUN J ...

  5. JDK 8与JDK 10:三元/拆箱的区别

    最近的Nicolai Parlog ( @nipafx ) 鸣叫引起了我的注意,因为它引用了关于JDK 8和JDK 10之间行为更改的有趣StackOverflow讨论 ,并询问"为什么?& ...

  6. 从JDK 8到JDK 17,GC都有哪些进步?

    作者 | Stefan Johansson 译者 | 弯月 出品 | CSDN(ID:CSDNnews) JDK17 发布已经几个月了,其中不仅包含很多新语言功能,而且与旧版 JDK 相比,性能提升也 ...

  7. JER与JDK区别以及JDK的安装配置(2021-06-13 Win10)

    JER与JDK区别以及JDK的安装配置 JRE与JDK JRE(Java Runtime Environment) ,是Java程序的运行环境,包含JVM和运行时所需要的核心类库,想要运行一个已有的J ...

  8. 如何安装JDK 15(其他JDK同样适用)

    JDK 15 安装教程 作者:龙龙 安装环境:Windows 10 专业版 X64 版本:2002 1.我们打开百度,搜索Java,进入Oracle官网: 找到Java SE 15,找到Oracle ...

  9. JDK是什么 JDK的作用

    原链接:https://product.pconline.com.cn/itbk/software/dnyw/1703/8922318.html JDK是学好Java的第一步.不管是你要学习java编 ...

最新文章

  1. uboot、kernel和rootfs烧录
  2. Android - Android Studio 解决访问被墙的问题
  3. 从头开发一个BurpSuite数据收集插件
  4. Lambda架构与推荐在电商网站实践
  5. 心态很容易受别人影响_阳光心态的句子,句句动人心弦,送给追梦路上的你
  6. Strom小实例,大小写转换
  7. 我的邮局系统,欢迎大家注册!hotxf.com
  8. 使用php,使用 PHP
  9. 十分钟搞定pandas+实战
  10. 三星note10 android q,【极光ROM】-【三星NOTE10/NOTE10+/5G N97XX-9825】-【V8.0 Android-Q-TJ4】...
  11. spring学习-01编译spring5.0源码(亲测可用)
  12. 直接收藏-超级好用的国内色彩搭配网站
  13. Android音量系统分析
  14. C++面向对象之类complex详解
  15. 没有捷径!没有捷径!没有捷径!
  16. centos安装MySQL到指定盘_Centos下安装mysql 和挂载硬盘
  17. verilog中tb仿真文件模板
  18. Feng Shui POJ - 3384
  19. Windows照片查看器无法显示此照片,因为计算机上的可用内存可能不足。
  20. IPv6地址—学习(一)

热门文章

  1. 年轮蛋糕JOI2014Final
  2. Vue中添加新的路由并访问
  3. [WinAPI] API 4 [注册][创建][消息][第一个框架类窗口]
  4. js 操作json对象增删改
  5. 玩转spring boot——结合阿里云持续交付
  6. ajax获取数据自动创建分页,支持自定义显示数据量以及分页数量
  7. 51CTO独家调查:谁是十年最具影响力厂商
  8. 比特币现金今日价格_比特币现金价格_今日比特币现金价格_06.12 上午 比特币现金价格 6060.87
  9. thinkphp 微信授权登录 以及微信实现分享
  10. iOS组件化开发实践