JDK中String类的源码分析(一)

JDK中String类的源码分析(一)

1、String类是final的,不允许被继承

1     /** The value is used for character storage. */
2     private final char value[];
3 
4     /** Cache the hash code for the string */
5     private int hash; // Default to 0

String类的内部就是维护了一个char数组;

2、构造方法,只需要看两个接受char数组的构造方法

复制代码

 1 public String(char value[]) {
 2         this.value = Arrays.copyOf(value, value.length);
 3     }
 4 
 5     public String(char value[], int offset, int count) {
 6         if (offset < 0) {
 7             throw new StringIndexOutOfBoundsException(offset);
 8         }
 9         if (count < 0) {
10             throw new StringIndexOutOfBoundsException(count);
11         }
12         // Note: offset or count might be near -1>>>1.
13         if (offset > value.length - count) {
14             throw new StringIndexOutOfBoundsException(offset + count);
15         }
16         this.value = Arrays.copyOfRange(value, offset, offset+count);
17     }

复制代码

这两个构造方法都用到了,Arrays工具类的copyOf方法,在这两个方法里面都调用了System.arraycopy方法;

因为System.arraycopy是一个系统本地方法,所以这个方法的效率很高,所以在构造String的时候效率也很高;

3、常用的length,charAt方法

通过第一条,很容易知道,这两个方法,实际上就是在操作char数组

复制代码

 1     public int length() {
 2         return value.length;
 3     }
 4 
 5     public boolean isEmpty() {
 6         return value.length == 0;
 7     }
 8 
 9     public char charAt(int index) {
10         if ((index < 0) || (index >= value.length)) {
11             throw new StringIndexOutOfBoundsException(index);
12         }
13         return value[index];
14     }

复制代码

4、getBytes方法

调用了StringCoding.encode方法,encode方法调用另外一个重载的方法

复制代码

 1     static byte[] encode(char[] ca, int off, int len) {
 2         String csn = Charset.defaultCharset().name();
 3         try {
 4             // use charset name encode() variant which provides caching.
 5             return encode(csn, ca, off, len);
 6         } catch (UnsupportedEncodingException x) {
 7             warnUnsupportedCharset(csn);
 8         }
 9         try {
10             return encode("ISO-8859-1", ca, off, len);
11         } catch (UnsupportedEncodingException x) {
12             // If this code is hit during VM initialization, MessageUtils is
13             // the only way we will be able to get any kind of error message.
14             MessageUtils.err("ISO-8859-1 charset not available: "
15                              + x.toString());
16             // If we can not find ISO-8859-1 (a required encoding) then things
17             // are seriously wrong with the installation.
18             System.exit(1);
19             return null;
20         }
21     }

复制代码

得到一个字节数组,是由ISO-8859-1编码得到,当然也可以用其他编码

5、compareTo方法

因为String实现了Comparable接口,所、所以必须实现compareTo方法

复制代码

 1     public int compareTo(String anotherString) {
 2         int len1 = value.length;
 3         int len2 = anotherString.value.length;
 4         int lim = Math.min(len1, len2);
 5         char v1[] = value;
 6         char v2[] = anotherString.value;
 7 
 8         int k = 0;
 9         while (k < lim) {
10             char c1 = v1[k];
11             char c2 = v2[k];
12             if (c1 != c2) {
13                 return c1 - c2;
14             }
15             k++;
16         }
17         return len1 - len2;
18     }

复制代码

其实是遍历的比较两个数组

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值