二 、在 JDK 6 and JDK 7中 substring() 方法
在JDK6 和JDK 7 里面substring(int beginIndex, int endIndex)的方法是不同的。知道这种区别会帮助你更好用它们。为了简单期间,下面用substring() 来表示 substring(int beginIndex,Int endIndex) 方法。
1、 substring()方法做什么
substring(int beginIndex,int endIndex)方法返回一个从beginIndex开始,到endIndex-1结束的字符串。
String x = "abcdef";
x = x.substring(1,3);
System.out.println(x);
输出:
bc
2、 当subString()方法被调用时候发生了什么?
你也许知道,因为x是不可变的,所以当x被赋值为x.substring(1,3)的时候,它指向了一个全新的字符串,如下图所示:
但是,这个图片没有正确的说明在堆里面发生的事情。在JDK6和JDK7 中调用substring()真正发生了什么。
3、 在JDK6中substring()
String 可以被标示成字符数组。在JDK6中,String 类包含三个属性:char value[], int offset,int count。它们被分别用于存储实际的字符串数组,数组第一个元素的标示,字符串中字符的个数。
当substring()方法被调用时候,它创建了一个新的字符串,但是这个字符串的值仍然指向在堆中相同的数组。原来字符串和新字符串的区别是他们的count和offset的值不同。
下面的代码是简化的,关于这个问题的关键解释。
//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、 在JDK6中substring()引起的问题
如果你有个非常长的字符串,但是你每次想通过用substring()来取其中一小部分。这将会产生一个性能问题,你仅仅需要一小部分,但是你保留了整个。在JDK6中,通过下面的方法,将使它指向一个真正的子串。
x = x.substring(x,y)+””
注意:推荐x = new String(x.substring(x, y)); 这样解决这个问题更好些。
5、 在JDK7中substring()
这些在JDK7中这些得到了改进。在JDK7中,substring()方法时间上在堆上创建了一个新的数组。
//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);
}
二 、在 JDK 6 and JDK 7中 substring() 方法的更多相关文章
- 菜鸟译文(三)——JDK6和JDK7中substring()方法的对比
substring(int beginIndex, int endIndex)方法在JDK6和JDK7中是不同的.了解他们的区别可以让我们更好的使用这个方法.方便起见,以下用substring() 代 ...
- String中substring方法内存泄漏问题
众所周知,JDK中以前String类中的substring方法存在内存泄漏问题,之所以说是以前,是因为JDK1.7及以后的版本已经修复了,我看都说JDK1.6的版本也存在这个问题,但是我本机上安装的1 ...
- JS中substring()方法(用于提取字符串中介于两个指定下标之间的字符)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- [译]JDK 6 and JDK 7中的subString()方法
(说明,该文章翻译自The substring() Method in JDK 6 and JDK 7) 在JDK 6 and JDK 7中的substring(int beginIndex, int ...
- MVC中动作方法三个特性以及解决同名方法冲突
一.Http请求谓词特性(解决方法同名冲突问题的一个方案) 关于Http谓词特点:经常使用,如果不加上该特性,默认动作方法接收所有谓词的请求一般开发中都会加上谓词,限定请求谓词类型 二.NonActi ...
- Spring学习总结(二)——静态代理、JDK与CGLIB动态代理、AOP+IoC
一.为什么需要代理模式 假设需实现一个计算的类Math.完成加.减.乘.除功能,如下所示: package com.zhangguo.Spring041.aop01; public class Mat ...
- [原]Jenkins(二)---jenkins之Git+maven+jdk+tomcat
/** * lihaibo * 文章内容都是根据自己工作情况实践得出. *版权声明:本博客欢迎转发,但请保留原作者信息! http://www.cnblogs.com/horizonli/p/5331 ...
- JDK学习---深入理解java中的HashMap、HashSet底层实现
本文参考资料: 1.<大话数据结构> 2.http://www.cnblogs.com/dassmeta/p/5338955.html 3.http://www.cnblogs.com/d ...
- JDK学习---深入理解java中的LinkedList
本文参考资料: 1.<大话数据结构> 2.http://blog.csdn.net/jzhf2012/article/details/8540543 3.http://blog.csdn. ...
随机推荐
- PHPstorm最常用的快捷键,提高开发效率
PHPstorm最常用的快捷键,提高开发效率 •ctrl+b 跳到变量申明处 •Ctrl + E 打开最近文件 •Ctrl + R 替换. •Ctrl + D 复制粘贴.将当前行或者选择的内容复制粘贴 ...
- angular.foEach
1.针对对象循环(key,value) 官方示例: var values = {name: 'misko', gender: 'male'}; var log = []; angular.forEac ...
- Linux下Tar压缩使用
具体的可以在linux环境下 用tar --help查看详细说明格式:tar [option] file -c create create a new archive -x extract extra ...
- Spring <context:annotation-config/> 说明
在基于主机方式配置Spring的配置文件中,你可能会见到<context:annotation-config/>这样一条配置,他的作用是式地向 Spring 容器注册AutowiredAn ...
- cydia源
http://apt.91.com http://apt.178.com Flex 2的官方安装源是 getdelta.co
- Hive之简单查询不启用MapReduce
假设你想查询某个表的某一列.Hive默认是会启用MapReduce Job来完毕这个任务,例如以下: 01 hive> SELECT id, money FROM m limit 10; 02 ...
- Socket实现服务器与客户端的交互
连接过程: 根据连接启动的方式以及本地套接字要连接的目标,套接字之间的连接过程可以分为三个步骤:服务器监听,客户端请求,连接确认. (1)服务器监听:是服务器端套接字并不定位具体的客户端套接 ...
- Linux下TCP最大连接数受限问题
一. 文件数限制修改1.用户级别查看Linux系统用户最大打开文件限制:# ulimit -n1024 (1) vi /etc/security/limits.confmysql soft nofil ...
- 如何在Win8中设置虚拟热点共享上网(转)
摘自:http://www.enet.com.cn/article/2013/0408/A20130408273749.shtml 在Windows 7中,我们可以通过网络与共享中心的“设置新的连接和 ...
- sudo: Cannot execute /usr/local/bin/zsh: No such file or directory 问题
参考:sudo: Cannot execute /usr/local/bin/zsh: No such file or directory 之前在美化Ubuntu的时候,下了个zsh,但是忘记改配置文 ...