When should and shouldn't instanceof be used?

后端 未结 5 631
情书的邮戳
情书的邮戳 2020-12-09 10:50

I have gone through various articles, but I still do not know why instanceof should not be used. Kindly let me know your thoughts.

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-09 11:07

    Using instanceof when overriding Object's equals method is necessary, seeing as two objects cannot be equal if they are of different types. I've rarely come accross another situation where instanceof is required - perhaps if downcasting is required but a ClassCastException may be thrown.

提交回复
热议问题