相关疑难解决方法(0)

Java中表达"new String(...)"的目的是什么?

在查看在线代码示例时,我有时会通过使用new运算符将String常量赋值给String对象.

例如:

String s;
...
s = new String("Hello World");
Run Code Online (Sandbox Code Playgroud)

当然,这与之相比

s = "Hello World";
Run Code Online (Sandbox Code Playgroud)

我不熟悉这种语法,也不知道目的或效果是什么.由于String常量通常存储在常量池中,然后以JVM处理String常量的任何表示形式存在,甚至还会在堆上分配任何内容?

java string

63
推荐指数
3
解决办法
2万
查看次数

为什么String类有复制构造函数?

可能重复:
Java中表达式"new String(...)"的目的是什么?

如果不可变类对象副本将等于原始副本那么为什么StringJava 中的类具有复制构造函数?这是一个错误还是这个实施背后的原因?在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) {
 ....
 ....}
Run Code Online (Sandbox Code Playgroud)

java

13
推荐指数
1
解决办法
1713
查看次数

标签 统计

java ×2

string ×1