• Post Reply Bookmark Topic Watch Topic
  • New Topic

equals() instanceof check? Check. null check also?  RSS feed

 
Julian West
Ranch Hand
Posts: 91
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've started OCP...

Sarbox..no, wait...Sybex wrote:equals() should return false when the object it passed in is not equal to the current object.  This includes null and incorrect types.

Got it.  Makes sense. 

What I don't understand is:
Sybex wrote:An equals() method should have a null check and an instanceof check.

Suspenders and a belt? 

Since null instanceof Class already returns false, isn't the null check already satisfied or is there some circumstance I'm missing?
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct. Null checks are redundant.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!