substring 的变异--jdk版本差异

来源:互联网 发布:sql 第几次出现 编辑:程序博客网 时间:2024/03/29 23:42

                substring 是我经常使用的方法 。

 如 : 

String a = "abcdef" ;

a = a.substring(1,3) ;

System.out.print(a) ;

result: bc

     接下来看看1.6版本里, 方法里做了什么 :

String ( int offset , int count, char value[]){

             this.offset = offset ,

             this.count = count ,

             this.value = value ;

}

 String substring(int begin , int end , int endIndex) {

             return new String (offset + begin , end - begin , value ) ;

}

   -----如果我们字符串存的是long long的文字内容  。 但是只取其中一小部分  ,调用api后, value的值并没有变。 内容还是位于内存之中 。

  1.7版本的,做了什么:

public String ( int offset , int count, char value[]){

          this.value = Arrays.copyOfRange( value , offset , offset+count) ;

}

String substring ( int begin , int end) {

             int subLen = end - begin ;

             return new String( value , begin , subLen) ;

}

 

   新版本里value的值变小了。   

原创粉丝点击