【jdk1.8】String源码分析

String

类的声明

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

首先可以看到String类是一个不变类,被final修饰,所以是不可继承的。
它实现了Serializable接口,还有Comparable(主要就是compareTo方法)与CharSequence(如下图)。
【jdk1.8】String源码分析_第1张图片

类的成员变量

    /** 底层字符的存储*/
    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;ibyte[] bt = test.getBytes(csn[i]);
            for(int j=0;jnew 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.
     * 

* A pool of strings, initially empty, is maintained privately by the * class {@code String}. *

* 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. *

* 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}. *

* All literal strings and string-valued constant expressions are * interned. String literals are defined in section 3.10.5 of the * The Java™ Language Specification. * * @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();

你可能感兴趣的:(合抱之木生于毫末)