码迷,mamicode.com
首页 > 其他好文 > 详细

"text"和new String("text")的区别

时间:2017-04-03 01:06:09      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:over   not   str   tin   pre   span   api   href   obj   

转自: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.

"text"和new String("text")的区别

标签:over   not   str   tin   pre   span   api   href   obj   

原文地址:http://www.cnblogs.com/drizzlewithwind/p/6652665.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!