tags:

views:

242

answers:

9

Is there any magic hanging around anywhere that could mean that

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

where object0 and object1 are both of a certain type which hasn't overridden Object.equals()?

A: 

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

Kekoa
+2  A: 

The Object.java src defines its equals method as;

 return (this == obj)

so no :-)

simon622
A: 

No. The actual class of object0 (not necessarily the declared type of the variable) must have overridden equals(). Try printing out object0.getClass().

Adam Crume
+15  A: 

No. That's exactly the definition of Object.equals().

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

public boolean equals( Object o ) { 
   return this == o;
}
OscarRyz
A: 

Here is the source code for Object.equals:

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

So, No.

chris
+7  A: 

Yes, if by "The type of object0 doesn't override Object.equals()" you mean the specific type and not a superclass.

If object0 and object1 are of type B, B extends A, and A overrides equals(Object obj) but B doesn't, then it is possible that B doesn't override equals(Object obj) but (object0 == object1) != (object0.equals(object1)).

+4  A: 

Well, if object0 == null and object1 == null, the first will deliver true, and the second a NullPointerException ;-) Apart from that, there should be no observeable difference.

mfx
+2  A: 

Although the objects don't override equals() themselves, it is possible that one of superclasses of the object overrides the equals() method...

If you are using eclipse: open the object.java file and press control-o twice. Type 'equals' and check if you only see one 'equals' method: the equals method of Object

Fortega
+1  A: 

Yes, null == null is true, but null.equals(null) is not defined.

egaga
@egaga. It IS defined ... to throw a NullPointerException!
Stephen C