Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

3
  • 1
    It's also 'important' or at least very useful as a 'best-practise' starting(or very early) line for for any Compare or Equals or similar method that is designed to only succeed on non-null objects of the same type, and guards you against the 'silly cases' in a single line. less code = less bugs. Commented Aug 15, 2013 at 9:47
  • 21
    To weigh in on the "is this useful?" debate - I've never written my own Java code (so don't easily know where the specs are, and compiling a test would be very non-trivial), but I'm currently manually converting Java to JavaScript. My code was failing on a null reference, and googling this let me see the accepted answer, which confirmed that it was expected behavior and that I was missing an implicit null check. Very useful, in my case. Commented Aug 27, 2013 at 15:31
  • I have leveraged the fact that instanceof checks for null to implement a very tight Java equals() implementation that reads way cleaner than those I see auto-generated by Eclipse and IntelliJ: stackoverflow.com/a/75402885/501113 Commented Feb 9, 2023 at 18:41