public class TestString {
public static void main(String [] args) {
String s1 = new String("hellow");
String s2 = "hellow";
String s3 = new String("hellow");
System.out.println("s1和s2 "+s1==s2);
...... 我不明白 为什么 "s1和s2 " 不打印出来呢???
public static void main(String [] args) {
String s1 = new String("hellow");
String s2 = "hellow";
String s3 = new String("hellow");
System.out.println("s1和s2 "+s1==s2);
...... 我不明白 为什么 "s1和s2 " 不打印出来呢???
你这样写就是 "s1和s2 "+s1 然后再和s2比较
System.out.println("s1和s2 "+(s1==s2));
String s1 = new String("hellow");//新建字符串池对象,新建堆中对象
String s2 = "hellow"; 已有字符串池对象,无需创建
String s3 = new String("hellow"); //新建堆中另一个对象,已有字符串池对象,无需创建