String s = new String("hello");
String t = "hello";
In above code, s is a String object and t is a String literal, that means 's == t ' is not true.
String literals are in 'constant pool' and are compiled into .class file. While String Object is dynamically constructed in running time.
Likewise, the a and b in the bellow code is not '==' each other but 'equals' each other.
String a = "";
String b = new String("");
String is an constant object, which means you can't change it's value. This is exactly why StringBuilder is introduced.
本文探讨了Java中字符串常量与字符串对象的区别,解释了为什么字符串常量池中的值与新创建的字符串对象使用'=='比较时返回false,而使用equals方法则返回true。同时介绍了字符串不可变性的概念及其带来的影响。
1091

被折叠的 条评论
为什么被折叠?



