有变量参与的情况下在对空间,没有变量参与的情况下一般在方法区的常量池中
@Test
public void test3(){
String s1 = "javaEE";
String s2 = "hadoop";
String s3 = "javaEEhadoop";
String s4 = "javaEE" + "hadoop";
String s5 = s1 + "hadoop";
String s6 = "javaEE" + s2;
String s7 = s1 + s2;
System.out.println(s3 == s4);//true
System.out.println(s3 == s5);//false
System.out.println(s3 == s6);//false
System.out.println(s3 == s7);//false
System.out.println(s5 == s6);//false
System.out.println(s5 == s7);//false
System.out.println(s6 == s7);//false
String s8 = s6.intern();//返回值得到的s8使用的常量值中已经存在的“javaEEhadoop”
System.out.println(s3 == s8);//true
}
public class Person {
String name;
public Person (String name) {
this.name = name;
}
}
public class PersonTest1 {
public static void main(String[] args) {
Person p1= new Person("Tom");
Person p2 = new Person("Tom");
System.out.println(p1.name == p2.name); //true
}
}