我知道的來源 「等於」,在Object類中的方法如下:Java中「equals」的含義是什麼?
public boolean equals(Object obj) {
return (this == obj);
}
下面是一個例子:
package equals;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
Object ObjA = new String("BBB");
Object ObjB = new String("BBB");
System.out.println(ObjA.equals(ObjB)); //the result is:true
System.out.println(ObjA == ObjB); //the result is:false
}
}
我知道這是真的,如果我改變
Object ObjA = new String("BBB");
Object ObjB = new String("BBB");
到
String strA = new String("BBB");
String strB = new String("BBB");
但是現在,我使用Object,我無法理解它。 誰能告訴我理由? 這就是!
這並不重要'Object'的執行'等於()'是。你的對象是'String'類型的。這是'String'的實現。 –
equals()方法匹配兩個對象並返回true或false –