<分区>
Possible Duplicate:
What is the purpose of the expression “new String(…)” in Java?
我知道应该避免 String s = new String("Hello World")
,因为它会为“Hello World”创建额外的空间,这在大多数情况下是不必要的。
解释为什么要避免 String s = new String("Hello World")
的相关问题在这里:
What is the difference between "text" and new String("text")?
但是我们什么时候需要使用 String s = new String("Hello World")
,而不是 String s = "Hello World"
?这是我遇到的一道面试题。
如果在大多数情况下应该避免 String s = new String("Hello World")
,为什么 Java 仍然允许这样做?