Java中的String对象不是不可变的吗?
String s = ...;
s = s.substring(1);
这可能吗?我认为你不能改变 Java 中的 String 对象。
String s = ...;
s = s.substring(1);
Is this possible? I thought you can't change a String object in Java.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
字符串对象是不可变的。然而,字符串引用是可变的。上面的
s
是一个参考。String objects are immutable. String references, however, are mutable. Above,
s
is a reference.字符串对象是不可变的,这意味着
s
引用的实例的值不能改变。您的代码不会改变实例。
相反,它会更改
s
引用以引用新的字符串实例。例如:
执行这段代码后,
b
仍然是"1"
。b = a
行将b
设置为引用a
当前引用的同一个"1"
实例。当您随后编写
a = "2"
时,您将更改a
变量以引用不同的 ("2"
) 实例。< br>但是,
b
仍然引用的原始"1"
实例并未改变。String objects are immutable, meaning that the value of the instance referred to by
s
cannot change.Your code does not mutate the instance.
Rather, it changes the
s
reference to refer to a new string instance.For example:
After executing this code,
b
is still"1"
.The line
b = a
setsb
to refer to the same"1"
instance thata
currently refers to.When you subsequently write
a = "2"
, you are changing thea
variable to refer to a different ("2"
) instance.However, the original
"1"
instance, whichb
still refers to, has not changed.是的,
字符串
对象是不可变的。变量
s
是一个对象的引用,并且该引用本身可以改变它所指向的对象——重新分配该引用不会影响它所指向的对象。String.substring
方法实际上返回一个String
的新实例,因此原始String
对象保持不变。下面是一个简单的示例,用于显示原始
String
没有被substring
方法更改::上面的示例将打印“Hello!”因为
substring
方法将返回一个新的String
,而不是影响原始字符串。原始的String
无法更改,因为它是不可变的。将上面的内容与下面的内容进行比较:
在这个示例中,对
s
的引用更改为substring
方法返回的String
,因此当与s
关联的String
由“System.out.println”打印,输出的字符串将为“ello!”Yes,
String
objects are immutable.The variable
s
is a reference to an object, and the reference itself can have the object it points to change -- reassigning the reference does not affect the object it points to.The
String.substring
method is actually returning a new instance of aString
, so the originalString
object is left untouched.The following is a simple example to show that the original
String
is not altered by thesubstring
method::The above example will print "Hello!" because the
substring
method will return a newString
rather than affect the original one. The originalString
cannot be altered because it is immutable.Compare the above with the following:
In this example, the reference to
s
is changed to theString
returned by thesubstring
method, so when theString
associated withs
is printed by `System.out.println", the string that is output will be "ello!"在这里,您将创建一个新字符串并将其分配给预先使用的引用。 s 引用的原始字符串被垃圾收集。实际上没有改变任何字符串。
Here you are creating a new string and assigning it to a pre-used reference. The original string that s referred to is garbage collected. No strings actually changed.
//创建对 String "Hello" 的引用
String s = "Hello";
//现在打印 s 引用的字符串的子字符串
System.out.println(s.subString(1) );
//现在打印 s 引用的字符串
System.out.println(s);
上面的代码片段将首先打印 H,然后 打印 H >第二行它将打印Hello。
现在为什么它首先打印 H? :事情是 subString() 方法返回一个字符串 *不更改引用 s 引用的字符串*。 s 仍然指的是“Hello”。现在,当您尝试打印 s 时,它将打印 s 所引用的字符串。这样字符串“Hello”是不可变的。您只能使用它来生成另一个字符串,但不能改变它。
When use statements s = s.subString(1);
您所做的是,s 现在引用“Hello”的 subString(),但“Hello”本身仍未修改。
//Create a reference s to String "Hello"
String s = "Hello";
//Now print subString of string referred by s
System.out.println(s.subString(1));
//Now print String referred by s
System.out.println(s);
The above code snippet would first print H and then on second line it will print Hello.
Now Why did it first print H? : Thing is subString() method returns a String *without changing the the string referred by reference s*. s is still referring to "Hello". Now when you try to print s it will print the string to which s is referring. in This way String "Hello" is immuatble. you are just able to use it to produce another string but you can not mutate it.
When use statement s = s.subString(1);
What you are doing is that s is now referring to a subString() of "Hello" but "Hello" itself is still not modified.
所以:
So:
当您使用 String s = "abc" 时,您将创建对具有不可变值“abc”的 String 对象的 String 引用。
然后,当您说 s = s.substring(1); 时,您将 s 分配给一个新创建的包含“bc”的 String 对象 - 但原始对象保持不变。
这是错误的常见原因,因为如果您没有分配该值,您可能会得到意外的结果。
许多Java新手开发人员会使用诸如trim()之类的方法,但没有意识到trim()不会影响字符串。
s.trim() <-- 对 s 不执行任何操作,返回修剪后的字符串 - 这是一个错误。
s = s.trim() <-- 存储修剪后的字符串 - 这是正确的。
When you use String s = "abc", you create a String reference to a String object that has the immutable value "abc".
Then, when you say s = s.substring(1);, you assign s to a newly created String object that contains "bc" - but the original object is unchanged.
This is a common cause of errors, because if you did not assign the value, you may get unexpected results.
Many novice Java developers will use such methods like trim(), not realizing that trim() doesn't affect the String.
s.trim() <-- Does nothing to s, returns a trimmed string - this is a bug.
s = s.trim() <-- Stores the trimmed string - this is correct.
测试一下:
s
现在是T
,j
仍然是Test
。Test it:
s
is nowT
andj
is stillTest
.