是“new String()”一成不变? [英] Is "new String()" immutable as well?

查看:97
本文介绍了是“new String()”一成不变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究Java String。以下问题基于以下帖子

I've been studying Java String for a while. The following questions are based on the below posts

Java字符串很特别

java中字符串的不变性


  1. 不变性:
    现在,由于不变性, String类的设计使得公共池中的值可以在其他位置/变量中重用。如果 String 创建为

  1. Immutability: Now, going by the immutability, the String class has been designed so that the values in the common pool can be reused in other places/variables. This holds good if the String was created as

字符串a =Hello World! ;
但是,如果我像

String a = "Hello World!"; However, if I create String like

字符串b =新字符串(Hello World !);
为什么这也是不可变的? (或者是吗?)。由于这有一个专用的堆内存,我应该能够修改它而不影响任何其他变量。因此,根据设计,是否有任何其他原因 String 作为一个整体被认为是不可变的?或者我的上述假设是错误的?

String b = new String("Hello World!"); why is this immutable as well? (or is it?). Since this has a dedicated heap memory, I should be able to modify this without affecting any other variable. So by design, was there any other reason why String as a whole is considered immutable? Or is my above assumption wrong?

我想问的第二件事是关于公共字符串池。如果我创建一个字符串对象为

Second thing I wanted to ask was about the common string pool. If I create a string object as

字符串c =;
是一个空条目创建的在游泳池?

String c = ""; is an empty entry created in the pool?

这些帖子上已有帖子吗?如果是这样,有人可以分享链接吗?

Is there any post already on these? If so, could someone share the link?

推荐答案

new String()是一个产生<$的表达式c $ c> String ...并且 String 是不可变的,无论它是如何产生的。

new String() is an expression that produces a String ... and a String is immutable, no matter how it is produced.

(询问 new String()是否可变是荒谬的。它是程序代码,而不是值。但我认为这不是你真正的意思。)

(Asking if new String() is mutable or not is nonsensical. It is program code, not a value. But I take it that that is not what you really meant.)


如果我创建了一个字符串对象为字符串c =; 是在池中创建的空条目?

If I create a string object as String c = ""; is an empty entry created in the pool?

是的;也就是说,为空字符串创建一个条目。空的字符串没有什么特别的。

Yes; that is, an entry is created for the empty string. There is nothing special about an empty String.

(要迂腐,在代码执行之前就已经创建了。实际上,它是在代码加载时创建的......或者甚至可能早于代码。)

(To be pedantic, the pool entry for "" gets created long before your code is executed. In fact, it is created when your code is loaded ... or possibly even earlier than that.)


所以,我想知道新的堆对象是否也是不可变的,。 ..

So, I was wanted to know whether the new heap object is immutable as well, ...

是的。但不变性是String对象的基本属性。所有字符串对象。

Yes it is. But the immutability is a fundamental property of String objects. All String objects.

你看,字符串 API根本不提供任何方法来更改 String 。所以(除了使用反射的一些危险和愚蠢的 1 技巧),你不能改变 String

You see, the String API simply does not provide any methods for changing a String. So (apart from some dangerous and foolish1 tricks using reflection), you can't mutate a String.


如果是,那么目的是什么?

and if so what was the purpose?.

Java String 设计为不可变类的原因是简单性。如果核心字符串类提供不可变的接口,它可以更容易地编写正确的程序,并读取/推理其他人的代码。 (至少,正如我所理解的那样,这就是这个设计决定的基本原理。)

The reason that Java String is designed as an immutable class is simplicity. It makes it easier to write correct programs, and read / reason about other people's code if the core string class provides an immutable interface. (Or at least, that is the rationale for this design decision, as I understand it.)


通过答案,我收集到了对同一变量的其他引用是其中一个原因。如果我理解这一点,请告诉我。

Going by the answer, I gather that other references to the same variable is one of the reasons. Please let me know if I am right in understanding this.

否。它比那更基础。简单地说,所有 String 对象都是不可变的。理解这一点并不需要复杂的特殊情况推理。它只是>>是<<。

No. It is more fundamental than that. Simply, all String objects are immutable. There is no complicated special case reasoning required to understand this. It just >>is<<.

对于记录,如果你想在Java中使用可变的字符串式对象,你可以使用 StringBuilder StringBuffer 。但这些是String的不同类型。

For the record, if you want a mutable "string-like" object in Java, you can use StringBuilder or StringBuffer. But these are different types to String.

1 - 这些技巧(IMO)危险和愚蠢的原因是它们会影响通过字符串池可能由应用程序的其他部分共享的字符串的值。这可能会造成混乱......以保持代码的下一个人很少有机会追踪。

这篇关于是“new String()”一成不变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆