String str = new String("SOME")
和 String str="SOME"
有什么区别
这些声明是否会产生性能变化。
最佳答案
String str = new String("SOME")
总是在堆上创建一个新对象
String str="SOME"
使用 String pool
试试这个小例子:
String s1 = new String("hello");
String s2 = "hello";
String s3 = "hello";
System.err.println(s1 == s2);
System.err.println(s2 == s3);
为避免在堆上创建不必要的对象,请使用第二种形式。
关于Java 字符串声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3652369/