13

Possible Duplicate:
Does Java guarantee that Object.getClass() == Object.getClass()?

I know you're supposed to use equals() in general, but is there any way two Class<?> objects could be equal with equals() but not equal with ==?

edit: I am specifically looking to find out whether two class objects exist such that

Class<?> cl1 = ...
Class<?> cl2 = ...
cl1.equals(cl2)    ->  true
cl1 == cl2         ->  false

This does not seemed to be covered by the possible duplicate question. (which is closely related)

Also it may not be true that the class objects were obtained by someObject.getClass() -- it could be that one was the result of Class.forName(...) and the other from some series of reflective actions like Method.getReturnType().

Community
  • 1
  • 1
Jason S
  • 184,598
  • 164
  • 608
  • 970
  • Exact duplicate looks like. Jason -- to find out in what instances the == and .equals return false, follow the link posted by @Goran – Kal May 13 '11 at 21:20
  • To those of you who closed: could you please either reopen or point out where in particular there is a case where == returns false but equals() returns true? – Jason S May 14 '11 at 14:21
  • 18
    Since `Class.equals()` is not implemented, `Class` uses the default implementation from `Object` which uses `==` for comparison. So for any two (non-null) `Class` references `c1` and `c2` `c1.equals(c2)` will return `true` **if and only if** `c1 == c2` is `true`. – Joachim Sauer May 16 '11 at 12:29
  • 2
    @Joachim: thanks. *That* is the kind of answer I was looking for (is this documented in the Java language spec or somewhere?) -- I still don't understand why this question was closed. – Jason S May 16 '11 at 12:36

1 Answers1

-2

All objects have both identity (the object's location in memory) and state (the object's data). The == operator always compares identity. The default implementation of equals compares identity as well.

For a fuller explanation: http://www.javapractices.com/topic/TopicAction.do?Id=17

Satish
  • 6,457
  • 8
  • 43
  • 63