

It’s still possible to encounter a NullPointerException, but it’s way harder if you use Clojure the way it’s intended. Only one instance of nil exists, and it tests to false. This article is about Java, so let us look at an example from another JVM language.Ĭlojure also prefers nil for the null reference.īut instead of providing a safe navigation operator to work around the problem, Clojure decided to try to get rid of the NullPointerException entirely.Įven though nil is kind of Java’s null, it’s just another value type. With its help, we can safely call properties, and even chain them fluently together: The language has excellent support for optional types and nil handling. In Swift, the null reference is called nil. How do other languages handle null? Swift Time for some different approaches to null handling. Other languages, like Objective-C, or Clojure, decided to ingrain better null handling. Groovy, Swift, Ruby, Kotlin, C#, and more support a safe navigation operator. Many of them have a proper way to handle null, directly integrated into the language itself. Programming languages are dealing very differently with the mistake. – Sir Charles Antony Richard Hoare at QCon London in 2009 This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years. My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler.īut I couldn’t resist the temptation to put in a null reference, simply because it was so easy to implement. It was the invention of the null reference in 1965.Īt that time, I was designing the first comprehensive type system for references in an object-oriented language (ALGOL W). The inventor of the null reference apologized in 2009 for its creation: The new class changed that significantly.īut first, some history and comparison with other languages. Until Java 8, there was no better way to handle null references than checking your variables at every turn.
