Java中的String对象不是不可变的吗?

发布于 2024-09-13 04:40:41 字数 102 浏览 5 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

南街女流氓 2024-09-20 04:40:41

字符串对象是不可变的。然而,字符串引用是可变的。上面的 s 是一个参考。

String objects are immutable. String references, however, are mutable. Above, s is a reference.

甜警司 2024-09-20 04:40:41

字符串对象是不可变的,这意味着 s 引用的实例的值不能改变。

您的代码不会改变实例。
相反,它会更改 s 引用以引用新的字符串实例。

例如:

String a = "1";
String b = a;
a = "2";

执行这段代码后,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:

String a = "1";
String b = a;
a = "2";

After executing this code, b is still "1".
The line b = a sets b to refer to the same "1" instance that a currently refers to.
When you subsequently write a = "2", you are changing the a variable to refer to a different ("2") instance.
However, the original "1" instance, which b still refers to, has not changed.

情绪 2024-09-20 04:40:41

是的,字符串 对象是不可变的。

变量s是一个对象的引用,并且该引用本身可以改变它所指向的对象——重新分配该引用不会影响它所指向的对象。

String.substring 方法实际上返回一个 String 的新实例,因此原始 String 对象保持不变。

下面是一个简单的示例,用于显示原始 String 没有被 substring 方法更改::

String s = "Hello!";
s.substring(1);

System.out.println(s);   // Prints "Hello!"

上面的示例将打印“Hello!”因为 substring 方法将返回一个新的String,而不是影响原始字符串。原始的 String 无法更改,因为它是不可变的。

将上面的内容与下面的内容进行比较:

String s = "Hello!";
s = s.substring(1);

System.out.println(s);   // Prints "ello!"

在这个示例中,对 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 a String, so the original String object is left untouched.

The following is a simple example to show that the original String is not altered by the substring method::

String s = "Hello!";
s.substring(1);

System.out.println(s);   // Prints "Hello!"

The above example will print "Hello!" because the substring method will return a new String rather than affect the original one. The original String cannot be altered because it is immutable.

Compare the above with the following:

String s = "Hello!";
s = s.substring(1);

System.out.println(s);   // Prints "ello!"

In this example, the reference to s is changed to the String returned by the substring method, so when the String associated with s is printed by `System.out.println", the string that is output will be "ello!"

高跟鞋的旋律 2024-09-20 04:40:41

在这里,您将创建一个新字符串并将其分配给预先使用的引用。 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.

帅哥哥的热头脑 2024-09-20 04:40:41

//创建对 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.

初心未许 2024-09-20 04:40:41

所以:

String foo = "foo";
foo.substring(1);
System.out.println(foo);

//of course..
foo = "aa";
System.out.println(foo);

So:

String foo = "foo";
foo.substring(1);
System.out.println(foo);

//of course..
foo = "aa";
System.out.println(foo);
痞味浪人 2024-09-20 04:40:41

当您使用 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.

寂寞陪衬 2024-09-20 04:40:41

测试一下:

String s = "Test";
String j = s;

s = s.substring(1);

s 现在是 Tj 仍然是 Test

Test it:

String s = "Test";
String j = s;

s = s.substring(1);

s is now T and j is still Test.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文