第一题
String s1 = “abc”;
String s2 = “abc”;
System.out.println(s1 == s2); // 答案为 true
Systrm.out.println(s1.equals(S2));// true
第二题
String s1 = new String(“abc”);
内存中创建了几个对象?
答: 2个 一个在常量池中 一个在堆内存中
第三题
String s1 = new String(“abc”);
String s2 = “abc”;
System.out.println(s1 == s2); // false 不相等
System.out.println(s1.equals(s2));/true
s1中记录的是堆内存对象的地址值
s2中记录的是常量池中的地址值
第四题
String s1 = “a” + “b” + “c” ;//常量池创建好abc
String s2 = “abc”; // 继续取常量池中的abc
System.out.println(s1 == s2);//true 因为java中的常量优化机制
System.out.println(s1.equals(s2));//true
第五题
String s1 = “ab”;
String s2 = “abc”;
String s3 = s1 + “c”;
System.out.println(s3 == s2);// false
System.out.println(s3.equals(s2));// true