开发者

java: comparing classes with == or .equals(): is there a difference? [duplicate]

This question already has answers here: Closed 11 years ago.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜