1 Equality for String adn Boolean objects means same charcter string String m1 = "Hello"; String m2 = "Hello"; m1.equals(m2) -----true; Boolean b1 = new Boolean(true); Boolean b2 = new Boolean(true); b1.equals(b2)------true; 2 Result of applying "==" operator to any two objects of any type: String s1 = "hi"; String s2 = "hi"; System.out.prilnt(s1 == s2);3 String s1 = new String("hi"); String s2 = new String("hi"); Boolean b1 = new Boolean(true); Boolean b2 = new Boolean(true); System.out.prilnt(s1 == s2); //false different reference System.out.prilnt(b1 == b2); //false different reference
一般 == 对比内存值,equals对比内容值是否相等 用法: String a = "A"; String b = "A"; a==b 返回 false a.equals(b) 返回 true 其它对象也一样(就是说大写开头的 类型,例如 Double,Integer,Float等等)而小写的类型(例如,int,double,float,boolean等等)就不能用equals比较了,只能用 == 号比较 int a = 1; int b = 1; a==b 返回 true a.equals(b)出错
String m1 = "Hello";
String m2 = "Hello";
m1.equals(m2) -----true;
Boolean b1 = new Boolean(true);
Boolean b2 = new Boolean(true);
b1.equals(b2)------true;
2 Result of applying "==" operator to any two objects of any type:
String s1 = "hi";
String s2 = "hi"; System.out.prilnt(s1 == s2);3 String s1 = new String("hi");
String s2 = new String("hi");
Boolean b1 = new Boolean(true);
Boolean b2 = new Boolean(true);
System.out.prilnt(s1 == s2); //false different reference
System.out.prilnt(b1 == b2); //false different reference
用法:
String a = "A";
String b = "A";
a==b 返回 false
a.equals(b) 返回 true
其它对象也一样(就是说大写开头的 类型,例如 Double,Integer,Float等等)而小写的类型(例如,int,double,float,boolean等等)就不能用equals比较了,只能用 == 号比较
int a = 1;
int b = 1;
a==b 返回 true
a.equals(b)出错