1、关于是否是同一个对象
package character;public class TestString { public static void main(String[] args) { String str1 = "the light"; String str2 = new String(str1); //==用于判断是否是同一个字符串对象 System.out.println( str1 == str2); }}str1和str2内容一样,但是不是同一个对象。
package character;public class TestString { public static void main(String[] args) { String str1 = "the light"; String str3 = "the light"; System.out.println( str1 == str3); }}所以在第6行会创建了一个新的字符串"the light"
但是在第7行,编译器发现已经存在现成的"the light",那么就直接拿来使用,而没有进行重复创建

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



