大家都说String不可变的原因是因为JDK源码中String类声明为final的原因.
那么我有一个问题:
既然String类声明为fianl就不可变.
那么StringBuffer类和StringBuilder也是被声明为final的.为什么他俩又可以变呢?

解决方案 »

  1.   

    大家都说String不可变的原因是因为JDK源码中String类声明为final的原因.
    -----------------------------------------------------------------------------
    final保证的只是不可被继承而已。
    String那特殊而变态的不变是String自己的实现造成的:
    StringBuffer和StringBuilder有append之类改变自己的方法,String所有改变自己的方法其实都new出了新类( ̄(工) ̄)
      

  2.   

    包括+操作,jvm里也通过StringBuffer和StringBuilder处理,最后new一个新的
      

  3.   

    关于String的不可改变的分析,以及与StringBuilder的区别。理解错误,String不可变是因为
    1 类自身是final的
    2 类里面的内容也是final的。比如最重要的保存字符串的部分
     private final char value[];而StringBuilder呢?
        public StringBuilder() {
    super(16);
        }
    在 AbstractStringBuilder 里面是
     char value[];
     AbstractStringBuilder(int capacity) {
       value = new char[capacity];
     }可见,这个char是可以变化的。通过一个 expandCapacity的方法进行扩充容量。
    这就是区别
      

  4.   


    //String 类中
     public String toUpperCase(Locale locale) {
    ......
    ......
            return new String(0, count+resultOffset, result);
        }
    //StringBuffer类中
    public synchronized StringBuffer append(String str) {
    super.append(str);
            return this;
        }
    //StringBuilder类中
    public StringBuilder append(String str) {
    super.append(str);
            return this;
        }是这样阿?~~
      

  5.   

    public String concat(String str) {
    int otherLen = str.length();
    if (otherLen == 0) {
        return this;
    }
    char buf[] = new char[count + otherLen];
    getChars(0, count, buf, 0);
    str.getChars(0, otherLen, buf, count);
    return new String(0, count + otherLen, buf);
        }public String replace(char oldChar, char newChar) {
    if (oldChar != newChar) {
        int len = count;
        int i = -1;
        char[] val = value; /* avoid getfield opcode */
        int off = offset;   /* avoid getfield opcode */     while (++i < len) {
    if (val[off + i] == oldChar) {
        break;
    }
        }
        if (i < len) {
    char buf[] = new char[len];
    for (int j = 0 ; j < i ; j++) {
        buf[j] = val[off+j];
    }
    while (i < len) {
        char c = val[off + i];
        buf[i] = (c == oldChar) ? newChar : c;
        i++;
    }
    return new String(0, len, buf);
        }
    }
    return this;
        }
    每个函数都不一样啊.?有的返回new的新对象,有的是对象本身.
      

  6.   

    public String replace(char oldChar, char newChar) {
    if (oldChar != newChar) {
        int len = count;
        int i = -1;
        char[] val = value; /* avoid getfield opcode */
        int off = offset;   /* avoid getfield opcode */     while (++i < len) {
    if (val[off + i] == oldChar) {
        break;
    }
        }
        if (i < len) {
    char buf[] = new char[len];
    for (int j = 0 ; j < i ; j++) {
        buf[j] = val[off+j];
    }
    while (i < len) {
        char c = val[off + i];
        buf[i] = (c == oldChar) ? newChar : c;
        i++;
    }
    return new String(0, len, buf);
        }
    }
    return this;
        }
      public String concat(String str) {
    int otherLen = str.length();
    if (otherLen == 0) {
        return this;
    }
    char buf[] = new char[count + otherLen];
    getChars(0, count, buf, 0);
    str.getChars(0, otherLen, buf, count);
    return new String(0, count + otherLen, buf);
        }
    每个函数都不一样吖?!有的是返回new的新对象 ,有的是返回this对象本身