我可以在不使用构造函数的情况下在 Java 中设置字符串的值吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3387831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I set the value of a String in Java without using a constructor?
提问by Crazy Chenz
How-Do/Can I set the value of a String object in Java (without creating a new String object)?
如何/能否在 Java 中设置 String 对象的值(不创建新的 String 对象)?
采纳答案by Mark Byers
There are no "set" methods on String
. Strings are immutablein Java. To change the value of a String
variable you need to assign a different string to the variable. You can't change the existing string.
上没有“设置”方法String
。字符串在 Java 中是不可变的。要更改String
变量的值,您需要为变量分配不同的字符串。您无法更改现有字符串。
(without creating a new String object)
(不创建新的 String 对象)
Assigning doesn't create a new object - it copies the reference. Note that even if you write something like this:
分配不会创建新对象 - 它会复制引用。请注意,即使你写这样的东西:
s = "hello";
it won'tcreate a new string object each time it is run. The string object will come from the string pool.
它不会在每次运行时创建一个新的字符串对象。字符串对象将来自字符串池。
回答by Mark Byers
Strings are immutable so you cannot change the value of an already created string.
字符串是不可变的,因此您无法更改已创建字符串的值。
回答by Jose Diaz
Actually there is no way to do that in Java, the String objects are immutable by default.
实际上在 Java 中没有办法做到这一点,默认情况下 String 对象是不可变的。
In fact, that's one of the reason why using the "+"
concatenation operator like "str1" + "str2"
is terribly inefficient, because what it does is copy every string in order to produce a third one.
事实上,这就是为什么使用"+"
连接运算符 like"str1" + "str2"
效率极低的原因之一,因为它所做的是复制每个字符串以生成第三个字符串。
Depending on your need you should consider using StringBuilder
根据您的需要,您应该考虑使用 StringBuilder
回答by extraneon
It depends a bit on your definition of object. If you mean the reference, no. A reference is always created. If you mean the memory used by the characters, sure.
这有点取决于您对对象的定义。如果你的意思是参考,没有。总是会创建一个引用。如果你的意思是字符使用的内存,当然。
Strings are interned (if possible) which means that in an assignment:
字符串是实习的(如果可能的话),这意味着在赋值中:
String s1 = "Hello";
String s2 = "Hello";
there are 2 references (pointers to a memory location), but Hello
is in memory on only 1 place. This is one of the reasons Strings can't be modified.
有 2 个引用(指向内存位置的指针),但Hello
在内存中只有 1 个位置。这是不能修改字符串的原因之一。
回答by Christian Ullenboom
Sure you can access the internal char array via reflection. But it's usually a bad idea to do so. More on http://www.eclipsezone.com/eclipse/forums/t16714.html.
当然你可以通过反射访问内部字符数组。但这样做通常是个坏主意。更多关于http://www.eclipsezone.com/eclipse/forums/t16714.html。
回答by Michael Shopsin
The String object is immutable in Java so any changes create a new String object. Use a StringBuilder if you want to make changes to a string like object without creating new objects. As a bonus the StringBuilder allows you to preallocate additional memory if you know something about the eventual length of your string.
String 对象在 Java 中是不可变的,因此任何更改都会创建一个新的 String 对象。如果您想在不创建新对象的情况下对类似字符串的对象进行更改,请使用 StringBuilder。作为奖励,如果您知道字符串的最终长度,StringBuilder 允许您预先分配额外的内存。
回答by dermoritz
I stumbled across this question because i have to set a string within an "enclosing type" - an anonymous type. But all variables i want to set inside and use outside must be final.
我偶然发现了这个问题,因为我必须在“封闭类型”中设置一个字符串 - 一种匿名类型。但是我想在内部设置并在外部使用的所有变量都必须是最终的。
The simple solution is to use StringBuilder- it's an mutable String.
简单的解决方案是使用 StringBuilder- 它是一个可变字符串。