编程语言
首页 > 编程语言> > Java源码赏析(二)初认 String 类

Java源码赏析(二)初认 String 类

作者:互联网

由于String类比较复杂,现在采用多篇幅来讲述

这一期主要从String使用的关键字,实现的接口,属性以及覆盖的方法入手。

 

/**
 * 精简的String结构,便于我们初步的理解
 * 使用了final关键字,说明此类是最终类,无法继承
 * 实现了序列化接口,排序接口,CharSequence接口
 */
public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence {

    private final char value[];

    private int hash; // Default to 0

    private static final long serialVersionUID =         -6849794470754667710L;

    private static final ObjectStreamField[] serialPersistentFields =
        new ObjectStreamField[0];

    /** 实现Comparable<String> */
    public int compareTo(String anotherString) {
        int len1 = value.length;
        int len2 = anotherString.value.length;
        int lim = Math.min(len1, len2);
        char v1[] = value;
        char v2[] = anotherString.value;

        int k = 0;
        while (k < lim) {
            char c1 = v1[k];
            char c2 = v2[k];
            if (c1 != c2) {
                return c1 - c2;
            }
            k++;
        }
        return len1 - len2;
    }


    /** 覆盖hashCode() */
    public int hashCode() {
        int h = hash;
        if (h == 0 && value.length > 0) {
            char val[] = value;

            for (int i = 0; i < value.length; i++) {
                h = 31 * h + val[i];
            }
            hash = h;
        }
        return h;
    }

    /** 覆盖父类equal() */
    public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        }
        if (anObject instanceof String) {
            String anotherString = (String)anObject;
            int n = value.length;
            if (n == anotherString.value.length) {
                char v1[] = value;
                char v2[] = anotherString.value;
                int i = 0;
                while (n-- != 0) {
                    if (v1[i] != v2[i])
                        return false;
                    i++;
                }
                return true;
            }
        }
        return false;
    }

    /** 覆盖父类toString() */
    public String toString() {
        return this;
    }

    /**
     *   实现CharSequence接口
     *   共有length(), charAt(int index), subSequence(int beginIndex, int endIndex) 等
     *   在实现subSequence()方法时使用了String中substring()方法
     */

    /** 获取字符串长度 */
    public int length() {
        return value.length;
    }

    /** 获取index位置的字符 */
    public char charAt(int index) {
        if ((index < 0) || (index >= value.length)) {
            throw new StringIndexOutOfBoundsException(index);
        }
        return value[index];
    }

    /** 获取子字符序列 */
    public CharSequence subSequence(int beginIndex, int endIndex) {
        return this.substring(beginIndex, endIndex);
    }

    
    /** 获取子串 */
    public String substring(int beginIndex, int endIndex) {
        if (beginIndex < 0) {
            throw new StringIndexOutOfBoundsException(beginIndex);
        }
        if (endIndex > value.length) {
            throw new StringIndexOutOfBoundsException(endIndex);
        }
        int subLen = endIndex - beginIndex;
        if (subLen < 0) {
            throw new StringIndexOutOfBoundsException(subLen);
        }
        return ((beginIndex == 0) && (endIndex == value.length)) ? this : new String(value, beginIndex, subLen);
    }
}     

 

未完待续

标签:beginIndex,return,String,int,value,初认,length,源码,赏析
来源: https://www.cnblogs.com/kwanwoo/p/13667737.html