一、String的创建
String s="hello";
JVM先根据内容"hello"查找对象,如果没有找到,则在heap上创建新对象,并将其赋予s1,否则使用已经存在的对象。
String s=new String("hello");
JVM直接在heap上创建新的对象,所以在heap中会出现内容相同,地址不同的String的对象。
二、String的比较
"=="是比较地址;
"equals"是比较内容。
举例如下:
String s1="hello";
String s2="hello";
String s3=new String("hello");
s1 == s2; //true 地址相同
s1 == s3; //false 地址不同
s1.equals(s2); //true 内容相同
s1.equals(s3); //true 内容相同