public class CharTest {
public static void main(String[] args) {
String s1=new String("abc");
String s2=new String("abc");
System.out.println(s1==s2);//比较的是俩个字符串变量引用是否指向同一个对象
System.out.println(s1.equals(s2));//比较俩个字符串对象的内容是是否相等
/*
* java字符串一旦定义将不能被修改,为了降低字符串的构造数量提高效率,
* 规定当没有使用显式构造对象时(即new String())时,
* 首先会到内存中查找是否存在着一个和当前要构造的对象的属性值相同的对象,如果存在则直接使用引用变量指向存在的对象,
* 否则重新创建一个
*/
String s3="abcd";
String s4="abcd";
//当使用显式构造对象时,即使存在属性值相同的对象也会重新创建一个对象
String s5=new String("abcd");
System.out.println(s3==s4);//true
System.out.println(s3.equals(s4));//true
System.out.println(s3==s5);//false
System.out.println(s3.equals(s5));//true
}
}
public static void main(String[] args) {
String s1=new String("abc");
String s2=new String("abc");
System.out.println(s1==s2);//比较的是俩个字符串变量引用是否指向同一个对象
System.out.println(s1.equals(s2));//比较俩个字符串对象的内容是是否相等
/*
* java字符串一旦定义将不能被修改,为了降低字符串的构造数量提高效率,
* 规定当没有使用显式构造对象时(即new String())时,
* 首先会到内存中查找是否存在着一个和当前要构造的对象的属性值相同的对象,如果存在则直接使用引用变量指向存在的对象,
* 否则重新创建一个
*/
String s3="abcd";
String s4="abcd";
//当使用显式构造对象时,即使存在属性值相同的对象也会重新创建一个对象
String s5=new String("abcd");
System.out.println(s3==s4);//true
System.out.println(s3.equals(s4));//true
System.out.println(s3==s5);//false
System.out.println(s3.equals(s5));//true
}
}