String的两种实例化方法的区别

来源:互联网 发布:c语言从1加到100while 编辑:程序博客网 时间:2024/06/08 14:30
String str1 = "hello";String str2 = "hello";String str3 = new String("hello");System.out.println(str1 == str2);System.out.println(str1 == str3);

output:
true
false

String str1 = “hello”;会将字符串数据放入对象池,下次再实例化该对象时可以直接使用
用new进行实例化,该字符串常量不会保存到对象池(可以使用intern()操作入池),另外还会产生额外垃圾空间,不建议使用。