String的subString()方法实现内部机制
2016-03-11 09:01
489 查看
在网上看到一篇关于java的String的subString()方法内部实现机制。现在将其记录下来。
JDK6:
调用substring()方法时创建的新的String与以前的string仍指向同一个char[]数组,只有offset(位置)以及count(计数)不同,由于char[]数组一直被引用,无法回收,所以可能导致内存泄漏。
JDK7:
JDK7在subString()时创建了一个新的数组,解决JDK6 subString()所带来的问题。
JDK6:
public final class String { /** The value is used for character storage. */ private final char value[]; /** The offset is the first index of the storage that is used. */ private final int offset; /** The count is the number of characters in the String. */ private final int count; public String(char value[], int offset, int count) { if (offset < 0) { throw new StringIndexOutOfBoundsException(offset); } if (count < 0) { throw new StringIndexOutOfBoundsException(count); } // Note: offset or count might be near -1>>>1. if (offset > value.length - count) { throw new StringIndexOutOfBoundsException(offset + count); } this.offset = 0; this.count = count; this.value = Arrays.copyOfRange(value, offset, offset+count); } public String substring(int beginIndex, int endIndex) { if (beginIndex < 0) { throw new StringIndexOutOfBoundsException(beginIndex); } if (endIndex > count) { throw new StringIndexOutOfBoundsException(endIndex); } if (beginIndex > endIndex) { throw new StringIndexOutOfBoundsException(endIndex - beginIndex); } return ((beginIndex == 0) && (endIndex == count)) ? this : new String(offset + beginIndex, endIndex - beginIndex, value); } }
调用substring()方法时创建的新的String与以前的string仍指向同一个char[]数组,只有offset(位置)以及count(计数)不同,由于char[]数组一直被引用,无法回收,所以可能导致内存泄漏。
JDK7:
public final class String { /** The value is used for character storage. */ private final char value[]; /** The offset is the first index of the storage that is used. */ private final int offset; /** The count is the number of characters in the String. */ private final int count; public String(char value[], int offset, int count) { if (offset < 0) { throw new StringIndexOutOfBoundsException(offset); } if (count < 0) { throw new StringIndexOutOfBoundsException(count); } // Note: offset or count might be near -1>>>1. if (offset > value.length - count) { throw new StringIndexOutOfBoundsException(offset + count); } this.value = Arrays.copyOfRange(value, offset, offset+count); } public String substring(int beginIndex, int endIndex) { if (beginIndex < 0) { throw new StringIndexOutOfBoundsException(beginIndex); } if (endIndex > value.length) { throw new StringIndexOutOfBoundsException(endIndex); } int subLen = endIndex - beginIndex; if (subLen < 0) { throw new StringIndexOutOfBoundsException(subLen); } return ((beginIndex == 0) && (endIndex == value.length)) ? this : new String(value, beginIndex, subLen); } }
JDK7在subString()时创建了一个新的数组,解决JDK6 subString()所带来的问题。
相关文章推荐
- 复杂性、易错性
- Extjs4.1 - Ext.util工具包详解
- Windows Embedded Standard 7 快速入门指南 1 of 4
- Android系统架构
- Linux下使用popen()执行shell命令
- Spring进阶之路(1)-Spring核心机制:依赖注入/控制反转
- Spring进阶之路(1)-Spring核心机制:依赖注入/控制反转
- Eclipse Plugin Development 从现在开始
- UVa489 Hangman Judge
- 在Eclipse中导出带有自己签名的Android apk文件
- 架构要素-高可用性
- win10系统桌面图标间距该怎么设置?
- CentOS 6.7网络配置
- 正则表达式学习参考
- 利用循环removeChild删除节点只删除一半问题
- 用js读写cookie的简单办法
- 【Oracle】【FAQ】Oracle SQL Developer 编辑区不能删除,后退,空格,复制粘贴等功能都失效的解决办法
- html5 localStorage(本地存储)
- java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener的解决办法
- CFILE C++获取当前路径 CSting 拼接