【发布时间】:2012-11-28 00:24:43
【问题描述】:
可能重复:
What is the purpose of the expression “new String(…)” in Java?
如果不可变类对象的副本等于原始对象,那么为什么 Java 中的 String 类有一个复制构造函数?这是一个错误还是此实施背后有原因?
在 Java 文档中指定:
/**
* Initializes a newly created {@code String} object so that it represents
* the same sequence of characters as the argument; in other words, the
* newly created string is a copy of the argument string. Unless an
* explicit copy of {@code original} is needed, use of this constructor is
* unnecessary since Strings are immutable.
*
* @param original
* A {@code String}
*/
public String(String original) {
....
....}
【问题讨论】:
-
这不是“过于本地化”,这是一个很好的问题,适用于后来发现它的人,答案非常有用且内容丰富。
-
见this 回答。
-
请注意,上述类似问题的答案现在可能被认为已部分过时。
标签: java