您的位置:首页 > 其它

String str = "hello"; 与 String str = new String("hello"); 有什么区别?

2016-03-31 10:10 459 查看
String str1 = "hello"; 如果已经为hello字符串分配了内存,那么str1指向hello内存对象的地址;如果没有就创建。

String str2 = "hello";

String str3 = "hello";

str1,str2,str3 这三个变量都是指向"hello"的地址,"hello"为三个变量共享。

String str4 = new String("hello");根据"hello"这个String对象再次构造一个String对象,将新构造出来的String对象的引用赋给str4,内存中实际存在两个"hello"对象,且存放的地址不一样。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: