靈魂拷問:java的String到底可不可變?

從認識java的那天起,就被告知String是不可變的,因爲源碼上是這樣寫的

public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence {
    /** The value is used for character storage. */
    private final char value[];

    /** 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;
    ……
    ……
    ……

很好理解,因爲被final關鍵字修飾了,所以是不可變的,但你能清楚的解釋下面的問題嗎

	  public static void main(String[] args) {
        String a = new String("abcd");
        String b = new String("abcd");
        String c = "abcd" + "ppp";
        String d = "abcd";
        String e = "abcd" + "ppp";
        String f = d + "ppp";
        
        System.out.println("情況1:"+(a == b) + "-------------" + a.equals(b));
        System.out.println("情況2:"+(a == d) + "-------------" + a.equals(d));
       
       
        System.out.println("情況4:"+(c == e) + "-------------" + c.equals(e));
        System.out.println("情況5:"+(f == e) + "-------------" + f.equals(e));
        a = a.intern();
        System.out.println("情況6:"+(a == d) + "-------------" + a.equals(d));
    }

是不是瘋了?

其實每種情況的後半段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;
    }

看上面的源碼知道String重寫的Object的eqals方法,本質是轉成字符數組然後逐一比較,所以上述6種情況後半段都是true


一個個來分析,情況1

 		String a = new String("abcd");
        String b = new String("abcd");
        System.out.println("情況1:"+(a == b) + "-------------" + a.equals(b));

看一下帶參數的構造函數的源碼:

    /**
     * Initializes a newly created {@code String} object so that it represents
     * the same sequence of characters as the argument; in other words, the
     * newly created string is a copy of the argument string. Unless an
     * explicit copy of {@code original} is needed, use of this constructor is
     * unnecessary since Strings are immutable.
     *
     * @param  original
     *         A {@code String}
     */
    public String(String original) {
        this.value = original.value;
        this.hash = original.hash;
    }

首先this.value是String定義的一個私有的被final修飾成員變量private final char value[];,將原始字符串的value和hash值進行賦值後,會返回一個String對象的引用,所以a和b返回的是兩個引用,自然不一樣,所以情況1前半段爲false


情況2

  String a = new String("abcd");
  String d = "abcd";
  System.out.println("情況2:"+(a == d) + "-------------" + a.equals(d));

看着好像是一樣的,但這個就涉及到String字符池的概念,其實在情況1中,jvm會在內部維護的String Pooll中放入一個"abc"對象,並在heap中創建一個String對象,然後將該heap中對象的引用返回給用戶
大概意思如下圖
在這裏插入圖片描述
堆內存和字符串有什麼不同呢,堆內存會隨着對象的回收而被GC回收,而字符池不會

所以執行String d = "abcd";的時候,實際上java的處理方式是先去字符池找,有沒有已經存在的字符串,如果有,則返回一個指向它的引用,如果沒有,則與new String(“abcd”)處理一樣,這裏可知,字符池中已經有,所以返回的只是一個String類型的引用,而===比較的就是引用,所以情況2的前半段結果也是false


情況3

	String b = new String("abcd");
    System.out.println("情況3:"+(b == d) + "-------------" + b.equals(d));
    String d = "abcd";

與情況2如出一轍,前半段結果也是false

情況4

  String c = "abcd" + "ppp";
  String e = "abcd" + "ppp";
  System.out.println("情況4:"+(c == e) + "-------------" + c.equals(e));

如果用+號來實現String的串接時:1)僅當+號兩邊均爲字符串常量時,纔將其+後的結果當做字符串常量,且該結果直接放入String Pool;2)若+號兩邊有一方爲變量時,+後的結果即當做非字符串常量處理(等同於new String()的效果) ,所以可知c和e引用的是用一個對象,所以情況4的前半段結果是true


情況5

		String d = "abcd";
        String e = "abcd" + "ppp";
        String f = d + "ppp";
	    System.out.println("情況5:"+(f == e) + "-------------" + f.equals(e));

根據情況4的分析,+號有一方爲變量,處理與new String()一致,自然前半段結果也是false

情況6

	    String a = new String("abcd");
		 a = a.intern();
        System.out.println("情況6:"+(a == d) + "-------------" + a.equals(d));

情況6與情況2唯一的不同就是a = a.intern();,那這個a = a.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();

大概意思就是如果常量池中存在當前字符串, 就會直接返回當前字符串. 如果常量池中沒有此字符串, 會將此字符串放入常量池中後, 再返回

所以a.intern()是顯式的調用了intern()方法,將a的引用由原來的指向heap中對象改爲指向內部維護的strings pool中的對像,a原來指向的String對象已經成爲垃圾對象了,隨時會被GC收集
如此一來自然前半段結果就是true了,真TM神奇

總結一下,String對象真的是不可變的,“可變的”是引用,jvm通過上面的策略,可以使多個引用指向一個對象而互不影響。字符池的存在當然是基於節約內存考慮。

最後我們來看一下結果
在這裏插入圖片描述
好像是全對了,100分,但是故事結束了嗎?

 public static String changeStr(String before) throws Exception{

        System.out.println("之前是這個---- "+before); //
        Field field = String.class.getDeclaredField("value");

        field.setAccessible(true);

        char[] value = (char[]) field.get(before);

        value[6] = 'J';

        value[7] = 'a';

        value[8] = 'v';

        value[9] = 'a';

        value[10] = '!';

        value[11] = '!';

        System.out.println("之後呀是這個---- "+before); 
      return before;

    }

    public static void main(String[] args) {
        try {

            System.out.println(changeStr("Hello String"));
        }catch (Exception e ){
            // 異常處理
        }

    }

在這裏插入圖片描述
啪,打臉,好疼!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章