1. String s1 = "hello" 和String s2 = new String("hello")有什么不同?
String s1 = "hello"; String s2 = "hello"; String s3 = new String("jixian"); String s4 = new String("jixian"); System.out.println(s1 == s2);//true System.out.println(s1 == s3);//false System.out.println(s1 == s4);//false System.out.println(s3 == s4);//false
String s1 = "hello"声明的字符串是在方法区的字符串常量池中,s1存的是字符串常量池中"hello"的地址;
而String s2 = new String("hello")的s2存的是堆空间中指向变量value的地址,而变量value的内容是字符串常量池中"hello"的地址。