java: comparing classes with == or .equals(): is there a difference? [duplicate]
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()
.
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
精彩评论