【jdk1.8】String源码分析

String

类的声明

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

首先可以看到String类是一个不变类,被final修饰,所以是不可继承的。
它实现了Serializable接口,还有Comparable(主要就是compareTo方法)与CharSequence(如下图)。
CharSequence

类的成员变量

    /** 底层字符的存储*/
    private final char value[];

    /** 哈希码*/
    private int hash; // Default to 0

类的构造方法

主要有三类类的构造方法,第一种是和byte[]相关的,第二种是和char[]相关的,第三种是和StringBuilder和StringBuffer相关的。

byte[]类

byte[]类里比较重要或者说比较常用的一个方法就是解码了。

public String(byte bytes[], String charsetName)
            throws UnsupportedEncodingException {
        this(bytes, 0, bytes.length, charsetName);
    }
    public String(byte bytes[], int offset, int length, String charsetName)
            throws UnsupportedEncodingException {
        if (charsetName == null)
            throw new NullPointerException("charsetName");
        checkBounds(bytes, offset, length);
        this.value = StringCoding.decode(charsetName, bytes, offset, length);
    }

我们来看一个例子:

        String test = "中文";
        String[] csn = new String[] {"ISO-8859-1", "GBK", "UTF-8"};
        for(int i=0;i<csn.length;i++){
            byte[] bt = test.getBytes(csn[i]);
            for(int j=0;j<csn.length;j++){
                String str = new String(bt, csn[j]);
                String res = new String(str.getBytes(csn[j]), csn[i]);
                System.out.print(res+"\t");
            }
            System.out.println();
        }

结果是:

        ISO GBK UTF-8
ISO     ??  ??  ??
GBK     中文  中文  锟斤拷锟斤拷
UTF-8   中文  中文  中文

为什么ISO-8859-1那一行编码组合再还原都不行呢?
因为ISO-8859-1编码的编码表中,没有包含汉字字符,当然也就无法通过["中文".getBytes("ISO8859-1");]来得到正确的”中文”在ISO-8859-1中的编码值了,所以再通过new String()来还原就无从谈起了。

char[] 和 StringXxx

主要就是Arrays.copyOf()的应用咯。

类的关键方法

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

其实就是公式s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]的值。

intern()

    /**本地方法*/    /**
     * Returns a canonical representation for the string object.
     * <p>
     * A pool of strings, initially empty, is maintained privately by the
     * class {@code String}.
     * <p>
     * When the intern method is invoked, if the pool already contains a
     * string equal to this {@code String} object as determined by
     * the {@link #equals(Object)} method, then the string from the pool is
     * returned. Otherwise, this {@code String} object is added to the
     * pool and a reference to this {@code String} object is returned.
     * <p>
     * It follows that for any two strings {@code s} and {@code t},
     * {@code s.intern() == t.intern()} is {@code true}
     * if and only if {@code s.equals(t)} is {@code true}.
     * <p>
     * All literal strings and string-valued constant expressions are
     * interned. String literals are defined in section 3.10.5 of the
     * <cite>The Java&trade; Language Specification</cite>.
     *
     * @return  a string that has the same contents as this string, but is
     *          guaranteed to be from a pool of unique strings.
     */
    public native String intern();
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值