sdbtongtong 2010-05-25 22:37 采纳率: 0%
浏览 237
已采纳

请问String的"不可变性"和"缓冲池特性"是什么意思是啊?

[size=large]我一直到今天都不知道什么意思
还有就是String a = new String("sdsd");
为什么是两个对象啊 究竟是哪两个对象啊[size]

  • 写回答

4条回答 默认 最新

  • iteye_5571 2010-05-25 23:46
    关注

    不可变性见string的源码 public final class String 可见string 对象是final的
    而且字符只要存储在一个char数组里面
    private final char value[]; 这个也是final,所以他是不可变的。
    public String(String original) {
    int size = original.count;
    char[] originalValue = original.value;
    char[] v;
    if (originalValue.length > size) {
    // The array representing the String is bigger than the new
    // String itself. Perhaps this constructor is being called
    // in order to trim the baggage, so make a copy of the array.
    int off = original.offset;
    v = Arrays.copyOfRange(originalValue, off, off+size);
    } else {
    // The array representing the String is the same
    // size as the String, so no point in making a copy.
    v = originalValue;
    }
    this.offset = 0;
    this.count = size;
    this.value = v;
    }
    这是string的构造函数,很明显可以看出,"sdsd"是参数也是一个String对象,然后new 了一个string对象a,所以是2个对象。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?