java: comparing classes with == or .equals(): is t

2019-02-16 19:58发布

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().

1条回答
啃猪蹄的小仙女
2楼-- · 2019-02-16 20:08

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

查看更多
登录 后发表回答