Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中的不可变字符串_Java_String - Fatal编程技术网

java中的不可变字符串

java中的不可变字符串,java,string,Java,String,我有基本的困惑 String s2=new String("immutable"); System.out.println(s2.replace("able","ability")); s2.replace("able", "abled"); System.out.println(s2); 在第一个print语句中,它正在打印不变性,但它是不变性的,对吗?为什么会这样? 和 在下一个打印语句中,未替换>任何答案欢迎..Stringreplace返回结果字符串,而不修改原始不可变字符串值 例如,

我有基本的困惑

String s2=new String("immutable");
System.out.println(s2.replace("able","ability"));
s2.replace("able", "abled");
System.out.println(s2);
在第一个print语句中,它正在打印不变性,但它是不变性的,对吗?为什么会这样? 和 在下一个打印语句中,未替换>任何答案欢迎..

Stringreplace返回结果字符串,而不修改原始不可变字符串值

例如,如果将结果分配给另一个字符串,则会得到相同的结果

String s2=new String("immutable");
String s3 = s2.replace("able","ability");
System.out.println(s3);
s2.replace("able", "abled");
System.out.println(s2);
会给你同样的输出

System.out.println(s2.replace("able","ability"));
在上面的一行中,返回并打印了一个新字符串

因为

返回一个新字符串,该字符串由newChar替换该字符串中所有出现的oldChar生成

它执行替换操作,但没有将结果赋回。因此原始字符串保持不变

如果指定结果,则可以看到结果

更新:

当你写这行的时候

System.out.println(s2.replace("able","ability"));
s2.replacement,ability解析并返回传递给该函数的字符串。

replaceString,String方法返回一个新字符串。第二次调用replace返回替换,但您没有将其分配给任何对象,然后当您再次打印不可变的s2时,您会看到未更改的值。

让我们看看第2行:

System.out.println(s2.replace("able","ability"));
这将打印不变性,这是因为

s2.replace("able","ability")
将返回另一个字符串,如下所示:

System.out.println(tempStr);
但在第三份声明中

s2.replace("able", "abled");
没有分配给另一个变量,因此返回一个字符串,但没有分配给任何变量。因此会丢失,但s2保持原样。

不可变对象只是对象的状态,对象的数据在构造后无法更改

你的代码s2。可替换,有能力,它返回一个新字符串,s2不会发生任何事情

因为replace函数返回一个新字符串,所以可以通过System.out.printlns2.replacement、ability打印结果

String是不可变的,但String有很多方法可以用作Rvalue

另见:

字符串s2=新字符串不可变

1当我们创建如上所述的字符串时,会创建一个新对象。如果我们试图修改它,则会使用我们提供的内容创建一个新对象,并且不会修改字符串2

2如果我们需要s2对象中的修改值,则将上述代码替换为

String s2=new String("immutable");//Creates a new object with content 'immutable'
System.out.println(s2.replace("able","ability"));//creates a new object with new content as //immutability
s2=s2.replace("able", "abled");//Here also it creates a new object,Since we are assigning it //to s2,s2 will be pointing to newly created object.
System.out.println(s2);//prints the s2 String value.

不可变和右值!本教程将帮助您更好地理解字符串不变性的概念。我没有得到的是什么@RONGNK将第二个replace语句的结果存储在一个变量中,更改将被反映出来。s2.replace确实创建了一个新字符串,但您没有存储任何where和原始对象保持不变。@如果设置字符串s3=s2.replace,则为Raj。可替换,可启用;你会得到结果的。您有以下一些解释。我不确定,但您确定没有生成新字符串吗?-1!它确实返回了一个新字符串,只是它没有分配给任何东西。即使在第二行中,我也没有分配给任何变量。我要添加Java中的字符串是不可变的,并且总是在更改时创建一个新字符串。
System.out.println(tempStr);
s2.replace("able", "abled");
String s2=new String("immutable");//Creates a new object with content 'immutable'
System.out.println(s2.replace("able","ability"));//creates a new object with new content as //immutability
s2=s2.replace("able", "abled");//Here also it creates a new object,Since we are assigning it //to s2,s2 will be pointing to newly created object.
System.out.println(s2);//prints the s2 String value.