码迷,mamicode.com
首页 > 其他好文 > 详细

String源码j简单分析

时间:2017-07-20 21:01:15      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:...   不可变   举例   getch   offset   color   valueof   unicode编码   ted   

分析:

1、

 private final char value[];

String内部由这个char数组维护String的字符。首先String类用final修饰,不可继承,其次,value[]用 fianl修饰,代表引用不可变。

    public String() {
        this.value = new char[0];
    }

当调用无参构造方法时,将char数组初始化为char[0]。

 

2、 String中的codePoint

codePoint  举例来说: “我”->对应的codePoint 为十进制的25105->十六进制的6211->UNICODE编码表中的6211(“我”字在UNICODE编码表中对应的16进制数)

3、   

public byte[] getBytes(String charsetName)
            throws UnsupportedEncodingException {
        if (charsetName == null) throw new NullPointerException();
        return StringCoding.encode(charsetName, value, 0, value.length);
    }

根据某编码格式编码

 

4、equals方法

 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;
    }

5、测试两个字符串区域是否相等。

    public boolean regionMatches(int toffset, String other, int ooffset,
            int len) {
        char ta[] = value;
        int to = toffset;
        char pa[] = other.value;
        int po = ooffset;
        // Note: toffset, ooffset, or len might be near -1>>>1.
        if ((ooffset < 0) || (toffset < 0)
                || (toffset > (long)value.length - len)
                || (ooffset > (long)other.value.length - len)) {
            return false;
        }
        while (len-- > 0) {
            if (ta[to++] != pa[po++]) {
                return false;
            }
        }
        return true;
    }

 6、hashcode

返回此字符串的哈希码。 String 对象的哈希码根据以下公式计算:

 s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
 

使用 int 算法,这里 s[i] 是字符串的第 i 个字符, n 是字符串的长度, ^ 表示求幂。(空字符串的哈希值为 0。)

    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;
    }

7、截取子串,返回的是new 的String

    public String substring(int beginIndex) {
        if (beginIndex < 0) {
            throw new StringIndexOutOfBoundsException(beginIndex);
        }
        int subLen = value.length - beginIndex;
        if (subLen < 0) {
            throw new StringIndexOutOfBoundsException(subLen);
        }
        return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);
    }

8、字符串拼接,返回的时new String,所以不建议多次拼接,多次拼接请选StringBuffer

    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);
    }

9、valueOf方法要注意,如果传进来的字符串为null,则会自动new String("null")返回,否则返回对象.toString()

    public static String valueOf(Object obj) {
        return (obj == null) ? "null" : obj.toString();
    }

 

String源码j简单分析

标签:...   不可变   举例   getch   offset   color   valueof   unicode编码   ted   

原文地址:http://www.cnblogs.com/lige-H/p/7213995.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!