In what circumstances can Java-referenced equality be different from equality equals () for an object of type that does not override equals ()?

Is there any magic hanging anywhere that could mean that

(object0 == object1) != (object0.equals(object1))

where object0 and object1 are defined types that have not redefined Object.equals ()?

+1
source share
9 answers

No. This is exactly the definition of Object. equals () .

... this method returns true if and only if x and y refer to the same object (x == y is true) ...

public boolean equals( Object o ) { 
   return this == o;
}
+16
source

Yes, if the type object0does not redefine Object.equals()"you mean a specific type, not a superclass.

object0 object1 B, B A, A equals(Object obj), B , , B equals(Object obj), (object0 == object1) != (object0.equals(object1)).

+8

, object0 == null object1 == null, true, - NullPointerException;-) , .

+5

equals(), , equals()...

eclipse: object.java control-o. "equals" , "equals": equals

+3

Object.java src equals :

 return (this == obj)

: -)

+2
source

Yes, null == nulltrue, but null.equals(null)not defined.

+1
source

No, if equals()not overridden, it returns true if the objects are the same identical objects in memory.

0
source

No. The actual object class 0 (not necessarily a declared variable type) must have overridden equals (). Try printing object0.getClass ().

0
source

Here is the source code for Object.equals:

public boolean equals(Object obj) {
  151           return (this == obj);
  152       }
  153   

So, no.

0
source