==
其实是在比地址,如果两个操作对象的地址是一样的,那么就返回ture
我们之前写的类似与String a = "hello", String b = "hello"
,系统发现他们两个内容一样,就会将a,b都指向同一个地址,如果用new
,两个地址就不同了
2.equals
是正儿八经在比类和值,也就是类的属性
只要类一样(必须是类,不能是子类),属性一样,就返回true
。
所有类都有equals()
这个函数,我们也可以改写这个函数
public class test
{
public static void main(String args[])
{
String a = "hello";
String b = "hello";
String a1 = new String("hello");
String b1 = new String("hello");
System.out.println(a == b); // a,b是同一个地址,返回 true
System.out.println( a1.equals(b1) ); // a1,b1值相等,返回 true
System.out.println(a.equals(b)); // a,b值相等,返回 true
System.out.println( a1 == b1 ); // a1,b1地址不同,返回 false
}
}