转自:What is the difference between “text” and new String(“text”)?

 

new String("text"); explicitly creates a new and referentially distinct instance of a Stringobject; String s = "text"; may reuse an instance from the string constant pool if one is available.

You very rarely would ever want to use the new String(anotherString) constructor.

From the API:

String(String original) : Initializes a newly created 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 original is needed, use of this constructor is unnecessary since strings are immutable.

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-08-05
  • 2021-10-06
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-31
  • 2022-12-23
  • 2021-06-18
  • 2021-07-13
  • 2022-12-23
相关资源
相似解决方案