jdk源码学习——String类

16 篇文章 0 订阅
9 篇文章 0 订阅

String类

在这里插入图片描述
String类被final所修饰,也就是说String对象是不可变量。String类实现了Serializable, Comparable, CharSequence接口。
Comparable接口有compareTo(String s)方法,CharSequence接口有length(),charAt(int index),subSequence(int start,int end)方法。

String类属性

    /** The value is used for character storage. */
    /**
     * 定义用于字符存储的value数组,由于使用了final修饰,
     * 表示该value[]一经初始化就不能进行修改
     */
    private final char value[];

    /**
     * 缓存字符串的hash值
     */
    /** Cache the hash code for the string */
    private int hash; // Default to 0

    /** use serialVersionUID from JDK 1.0.2 for interoperability */
    private static final long serialVersionUID = -6849794470754667710L;

String类构造函数

/**
     * 初始化一个新创建的{@code String}对象,使其表示一个空字符序列。注意,由于字符串是不可变的,因此不需要使用此构造函数。
     */
    public String() {
        this.value = "".value;
    }

    /**
     * 使用字符串构造函数
     * @param original
     */
    public String(String original) {
        this.value = original.value;
        this.hash = original.hash;
    }

   
    /**
     * 字符串数组构造函数
     * @param value
     */
    public String(char value[]) {
        this.value = Arrays.copyOf(value, value.length);
    }

   
    /**
     * offset表示第一个被截取的字符在数组value[]中的下标,count表示从此字符开始向后截取的字符的数量
     * 将value[]数组按照传入的下标和指定的截取数组数据的数量进行截取,并且创建一个内容为此的string对象
     * @param value
     * @param offset
     * @param count
     */
    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);
    }

    
    /**
     * 从数组下标为offset开始顺序取出数组codePoints[]中count个整数,并根据此uncode代码点所对应的字符创建一个string对象
     * @param codePoints
     * @param offset
     * @param count
     */
    public String(int[] codePoints, int offset, int count) {
        if (offset < 0) {
            throw new StringIndexOutOfBoundsException(offset);
        }
        if (count <= 0) {
            if (count < 0) {
                throw new StringIndexOutOfBoundsException(count);
            }
            if (offset <= codePoints.length) {
                this.value = "".value;
                return;
            }
        }
        // Note: offset or count might be near -1>>>1.
        if (offset > codePoints.length - count) {
            throw new StringIndexOutOfBoundsException(offset + count);
        }

        final int end = offset + count;

        // Pass 1: Compute precise size of char[]
        /**
         * Pass 1: Compute(计算) precise(精确的) size of char[] 用于下面创建v数组时的数组长度的确定
         */
        int n = count;
        for (int i = offset; i < end; i++) {
            int c = codePoints[i];
            //判断代码点是不是BMP(此方法在1.6之前没有)
            if (Character.isBmpCodePoint(c))
                continue;
            //确定指定的代码点是否是一个有效的Unicode代码点值
            else if (Character.isValidCodePoint(c))
                n++;
            else throw new IllegalArgumentException(Integer.toString(c));
        }

        // Pass 2: Allocate(分配) and fill in(填充) char[]
        // Pass 2: Allocate and fill in char[]
        final char[] v = new char[n];//用于存储要转成string对象中字符的字符

        for (int i = offset, j = 0; i < end; i++, j++) {
            int c = codePoints[i];
            if (Character.isBmpCodePoint(c))
                v[j] = (char)c;
            else
                Character.toSurrogates(c, v, j++);
        }

        this.value = v;
    }

String类的常用方法

equals

public boolean equals(Object anObject) {
        /**
         * 如果引用是同一个对象,返回真
         */
        if (this == anObject) {
            return true;
        }
        //如果不是String类型的数据,返回假
        if (anObject instanceof String) {
            String anotherString = (String)anObject;
            int n = value.length;
            //如果char数组长度不相等,返回假
            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;
    }
    /**
     * equals方法经常用得到,它用来判断两个对象从实际意义上是否相等,String对象判断规则:
     *
     * 内存地址相同,则为真。
     *
     * 如果对象类型不是String类型,则为假。否则继续判断。
     *
     * 如果对象长度不相等,则为假。否则继续判断。
     *
     * 从后往前,判断String类中char数组value的单个字符是否相等,有不相等则为假。如果一直相等直到第一个数,则返回真。
     *
     * 由此可以看出,如果对两个超长的字符串进行比较还是非常费时间的。
     */

compareTo

public int compareTo(String anotherString) {
        //自身对象字符串长度len1
        int len1 = value.length;
        //被比较对象的字符串长度len2
        int len2 = anotherString.value.length;
        //取两个字符串长度的最小值lim
        int lim = Math.min(len1, len2);
        char v1[] = value;
        char v2[] = anotherString.value;

        int k = 0;
        //从value的第一个字符开始到最小长度lim处为止,如果字符不相等,返回自身(对象不相等处字符-被比较对象不相等字符)
        while (k < lim) {
            char c1 = v1[k];
            char c2 = v2[k];
            if (c1 != c2) {
                return c1 - c2;
            }
            k++;
        }
        //如果前面都相等,则返回(自身长度-被比较对象长度)
        return len1 - len2;
    }
    /**
     * 这个方法写的很巧妙,先从0开始判断字符大小。如果两个对象能比较字符的地方比较完了还相等,
     * 就直接返回自身长度减被比较对象长度,如果两个字符串长度相等,则返回的是0,巧妙地判断了三种情况。
     */

hashCode

/**
     * Returns a hash code for this string. The hash code for a
     * {@code String} object is computed as
     * <blockquote><pre>
     * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
     * </pre></blockquote>
     * using {@code int} arithmetic, where {@code s[i]} is the
     * <i>i</i>th character of the string, {@code n} is the length of
     * the string, and {@code ^} indicates exponentiation.
     * (The hash value of the empty string is zero.)
     *
     * @return  a hash code value for this object.
     */
    public int hashCode() {
        int h = hash;
        //如果hash没有被计算过,并且字符串不为空,则进行hashCode计算
        if (h == 0 && value.length > 0) {
            char val[] = value;


            //计算过程
            //s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
            for (int i = 0; i < value.length; i++) {
                h = 31 * h + val[i];
            }
            //hash赋值
            hash = h;
        }
        return h;
    }
    /**
     * String类重写了hashCode方法,Object中的hashCode方法是一个Native调用。String类的hash采用多项式计算得来,
     * 我们完全可以通过不相同的字符串得出同样的hash,所以两个String对象的hashCode相同,并不代表两个String是一样的。
     */

concat

/**
     * concat方法也是经常用的方法之一,它先判断被添加字符串是否为空来决定要不要创建新的对象。
     * @param str
     * @return
     */
    public String concat(String str) {
        int otherLen = str.length();
        //如果被添加的字符串为空,返回对象本身
        if (otherLen == 0) {
            return this;
        }
        int len = value.length;
        char buf[] = Arrays.copyOf(value, len + otherLen);
        str.getChars(buf, len);
        return new String(buf, true);
    }

replace

/**
     * 这个方法也有讨巧的地方,例如最开始先找出旧值出现的位置,这样节省了一部分对比的时间。
     * @param oldChar
     * @param newChar
     * @return
     */
    public String replace(char oldChar, char newChar) {
        //新旧值先对比
        if (oldChar != newChar) {
            int len = value.length;
            int i = -1;
            char[] val = value; /* avoid getfield opcode */

            //找到旧值最开始出现的位置
            while (++i < len) {
                if (val[i] == oldChar) {
                    break;
                }
            }
            //从那个位置开始,直到末尾,用新值代替出现的旧值
            if (i < len) {
                char buf[] = new char[len];
                for (int j = 0; j < i; j++) {
                    buf[j] = val[j];
                }
                while (i < len) {
                    char c = val[i];
                    buf[i] = (c == oldChar) ? newChar : c;
                    i++;
                }
                return new String(buf, true);
            }
        }
        return this;
    }

trim

public String trim() {
        int len = value.length;
        int st = 0;
        char[] val = value;    /* avoid getfield opcode */

        //找到字符串前段没有空格的位置
        while ((st < len) && (val[st] <= ' ')) {
            st++;
        }
        //找到字符串末尾没有空格的位置
        while ((st < len) && (val[len - 1] <= ' ')) {
            len--;
        }
        //如果前后都没有出现空格,返回字符串本身
        return ((st > 0) || (len < value.length)) ? substring(st, len) : this;
    }

intern

/**
     * intern方法是Native调用,它的作用是在方法区中的常量池里通过equals方法寻找等值的对象,
     * 如果没有找到则在常量池中开辟一片空间存放字符串并返回该对应String的引用,否则直接返回常量池中已存在String对象的引用。
     * @return
     */
    public native String intern();

其他常用的简单方法

public int length() {
    return value.length;
}
public String toString() {
    return this;
}
public boolean isEmpty() {
    return value.length == 0;
}
public char charAt(int index) {
    if ((index < 0) || (index >= value.length)) {
        throw new StringIndexOutOfBoundsException(index);
    }
    return value[index];
}

总结

String对象是不可变类型,返回类型为String的String方法每次返回的都是新的String对象,除了某些方法的某些特定条件返回自身。

String对象的三种比较方式:

==内存比较:直接对比两个引用所指向的内存值,精确简洁直接明了。

equals字符串值比较:比较两个引用所指对象字面值是否相等。

hashCode字符串数值化比较:将字符串数值化。两个引用的hashCode相同,不保证内存一定相同,不保证字面值一定相同。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值