JAVA 源码之路01 String源码

2018-08-28  本文已影响0人  kekeer

1. 总观

String 类作为JAVA中最经常使用的一个类,放在了第一位来学习。
String类位于java.lang.String,继承关系为

    public final class String implements java.io.Serializable, Comparable<String>, CharSequence

其中继承了三个接口

2. 属性

    private final char value[];
    private int hash;
    public static final Comparator<String> CASE_INSENSITIVE_ORDER
        = new CaseInsensitiveComparator();

可以看出在compare方法中进行了大小写的转换来比较

3. 构造方法

String的构造方法还是比较多的

 public String() {
     this.value = "".value;
 }
 public String(String original) {
     this.value = original.value;
     this.hash = original.hash;
 }
 public String(char value[]) {
     this.value = Arrays.copyOf(value, value.length);
 }
 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(StringBuffer buffer) {
     synchronized(buffer) {
         this.value = Arrays.copyOf(buffer.getValue(), buffer.length());
     }
 }
 public String(StringBuilder builder) {
     this.value = Arrays.copyOf(builder.getValue(), builder.length());
 }

4. 基本方法

上一篇下一篇

猜你喜欢

热点阅读