不可变到底是什么意思——也就是说,对象可变或不可变的结果是什么?特别是,为什么Java的字符串是不可变的?

我的理解是StringBuilder类型类似于String的可变等价。什么时候我会使用StringBuilder而不是字符串,反之亦然?


当前回答

String s1="Hi";
String s2=s1;
s1="Bye";

System.out.println(s2); //Hi  (if String was mutable output would be: Bye)
System.out.println(s1); //Bye

s1="Hi":创建一个包含"Hi"值的对象s1。

S2 =s1:引用s1对象创建对象S2。

s1="Bye":之前的s1对象的值不会改变,因为s1有String类型,而String类型是一个不可变的类型,相反,编译器创建了一个新的String对象与"Bye"值和s1引用它。在这里,当我们打印s2值时,结果将是“Hi”而不是“Bye”,因为s2引用了前一个具有“Hi”值的s1对象。

其他回答

不可变对象是指创建后不能修改的对象。一个典型的例子是字符串字面量。

越来越流行的D编程语言通过“不变”关键字具有“不变性”的概念。查看Dr.Dobb关于它的文章http://dobbscodetalk.com/index.php?option=com_myblog&show=Invariant-Strings.html&Itemid=29。它完美地解释了这个问题。

不可变意味着一旦一个对象的构造函数完成执行,该实例就不能被改变。

这很有用,因为这意味着你可以传递对对象的引用,而不用担心其他人会改变它的内容。特别是在处理并发性时,对于永不更改的对象不存在锁定问题

e.g.

class Foo
{
     private final String myvar;

     public Foo(final String initialValue)
     {
         this.myvar = initialValue;
     }

     public String getValue()
     {
         return this.myvar;
     }
}

Foo不必担心getValue()的调用者可能会更改字符串中的文本。

如果你想象一个类似于Foo的类,但是成员是StringBuilder而不是String,你可以看到getValue()的调用者能够改变Foo实例的StringBuilder属性。

还要注意你可能会发现的不同类型的不变性:Eric Lippert写了一篇关于这个的博客文章。基本上,你可以拥有接口是不可变的对象,但在幕后实际可变的私有状态(因此不能在线程之间安全地共享)。

不可变对象是内部字段(或者至少是影响其外部行为的所有内部字段)不能被更改的对象。

不可变字符串有很多优点:

性能:执行如下操作:

String substring = fullstring.substring(x,y);

substring()方法的底层C可能是这样的:

// Assume string is stored like this:
struct String { char* characters; unsigned int length; };

// Passing pointers because Java is pass-by-reference
struct String* substring(struct String* in, unsigned int begin, unsigned int end)
{
    struct String* out = malloc(sizeof(struct String));
    out->characters = in->characters + begin;
    out->length = end - begin;
    return out;
}

注意,没有一个字符必须被复制!如果String对象是可变的(字符可以在以后更改),那么您将不得不复制所有字符,否则对子字符串中的字符的更改将在以后反映到另一个字符串中。

并发性:如果一个不可变对象的内部结构是有效的,那么它将总是有效的。不同的线程不可能在该对象中创建无效状态。因此,不可变对象是线程安全的。

垃圾收集:垃圾收集器更容易对不可变对象做出逻辑决策。

然而,不可变性也有缺点:

性能:等等,我记得你说过性能是不变性的好处!有时候是这样,但不总是这样。取以下代码:

foo = foo.substring(0,4) + "a" + foo.substring(5);  // foo is a String
bar.replace(4,5,"a"); // bar is a StringBuilder

The two lines both replace the fourth character with the letter "a". Not only is the second piece of code more readable, it's faster. Look at how you would have to do the underlying code for foo. The substrings are easy, but now because there's already a character at space five and something else might be referencing foo, you can't just change it; you have to copy the whole string (of course some of this functionality is abstracted into functions in the real underlying C, but the point here is to show the code that gets executed all in one place).

struct String* concatenate(struct String* first, struct String* second)
{
    struct String* new = malloc(sizeof(struct String));
    new->length = first->length + second->length;

    new->characters = malloc(new->length);
    
    int i;

    for(i = 0; i < first->length; i++)
        new->characters[i] = first->characters[i];

    for(; i - first->length < second->length; i++)
        new->characters[i] = second->characters[i - first->length];

    return new;
}

// The code that executes
struct String* astring;
char a = 'a';
astring->characters = &a;
astring->length = 1;
foo = concatenate(concatenate(slice(foo,0,4),astring),slice(foo,5,foo->length));

注意,concatenate被调用两次,这意味着整个字符串必须循环!将其与bar操作的C代码进行比较:

bar->characters[4] = 'a';

可变字符串操作显然要快得多。

总结:在大多数情况下,您需要一个不可变的字符串。但是如果你需要在一个字符串中做大量的追加和插入,你就需要可变性来提高速度。如果你想要并发安全性和垃圾收集的好处,关键是保持你的可变对象本地的方法:

// This will have awful performance if you don't use mutable strings
String join(String[] strings, String separator)
{
    StringBuilder mutable;
    boolean first = true;

    for(int i = 0; i < strings.length; i++)
    {
        if(first) first = false;
        else mutable.append(separator);

        mutable.append(strings[i]);
    }

    return mutable.toString();
}

因为可变对象是一个本地引用,所以不必担心并发安全性(只有一个线程接触过它)。由于它没有在其他任何地方被引用,所以它只在堆栈上分配,所以函数调用一结束它就会被释放(您不必担心垃圾收集)。你可以同时获得可变性和不可变性的性能优势。

一旦实例化,就不能更改。考虑一个类,它的实例可能用作哈希表或类似的键。查看Java最佳实践。

不可改变的意思是不可改变的。字符串对象一旦创建,其数据或状态就不能更改

考虑下面的例子:

class Testimmutablestring{  
  public static void main(String args[]){  
    String s="Future";  
    s.concat(" World");//concat() method appends the string at the end  
    System.out.println(s);//will print Future because strings are immutable objects  
  }  
 }  

让我们考虑一下波纹图,

在这个图中,你可以看到一个新对象被创建为“Future World”。但不改变“未来”。因为字符串是不可变的。s,仍指“未来”。如果你需要给“未来世界”打电话,

String s="Future";  
s=s.concat(" World");  
System.out.println(s);//print Future World

为什么在java中字符串对象是不可变的?

因为Java使用了字符串字面量的概念。假设有5个引用变量,都指向一个对象“Future”。如果一个引用变量改变了对象的值,它将影响到所有的引用变量。这就是为什么字符串对象在java中是不可变的。