JDK String 源码学习,这个构造方法写的有点逻辑不太清晰
2016-01-25 16:39
519 查看
今天无意间看到String的一个构造方法,总感觉有点不是太舒服。源码如下(JDK 1.8 ):
改成如下这种不知道是不是考虑周全了:
public String(char value[], int offset, int count) { if (offset < 0) { throw new StringIndexOutOfBoundsException(offset); } if (count <= 0) { if (count < 0) { throw new StringIndexOutOfBoundsException(count); } if (offset <= value.length) { this.value = "".value; return; } } // 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(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); } //necessary? if (count == 0) { this.value = "".value; return; } this.value = Arrays.copyOfRange(value, offset, offset + count); }
相关文章推荐
- Android之获取手机上的图片和视频缩略图thumbnails
- android string.xml文件中的整型和string型代替
- Android java 与 javascript互访(相互调用)的方法例子
- 从源码安装Mysql/Percona 5.5
- android上改变listView的选中颜色
- String.intern
- Ubuntu 安装 JDK 问题
- Prototype源码浅析 String部分(二)
- Ruby中的String对象学习笔记
- 浅析Ruby的源代码布局及其编程风格
- PostgreSQL ERROR: invalid escape string 解决办法
- 浅谈C++中的string 类型占几个字节
- 标准C++类string的Copy-On-Write技术
- C++实现string存取二进制数据的方法
- C#中string和StingBuilder内存中的区别实例分析
- PHP STRING 陷阱原理说明
- c#中 String和string的区别介绍
- C#实现的图片、string相互转换类分享
- asp.net String.IsNullOrEmpty 方法
- asp.net 抓取网页源码三种实现方法