NULL handling in Java vs Kotlin

Overview

One of the most common pitfalls in many programming languages, including Java, is that accessing a member of a null reference will result in a null reference exception. Every Java programer is familiar with the NullPointerException.

NULL handling in Java

Before Optional (Java 8) the only way to check whether an object is null or not is to check explicitly

if (obj != null) {
  //Do stuffs
}

Let’s say we have a User object which has an Address. Address has a State object. State has the String value of the state. The way to get the name of the state in this case is to check every object if it’s present or not:

if (user != null && user.getAddress() != null && user.getAddress().getState() != null) {
  return user.getAddress().getState().getName();
}

Java 8 brought a new way to handle this by using Optional. It is a container object which has the real object.

NULL handling in Kotlin

Kotlin’s type system is aimed at eliminating the danger of null references from code. In Kotlin, the type system distinguishes between references that can hold null (nullable references) and those that can not (non-null references). For example, a regular variable of type String can not hold null:

var notNull: String = "abc" // Regular initialization means non-null by default
notNull = null // compilation error

So if you want to call a method on object notNull, you are sure that it is not null. To allow nulls, we can declare a variable as nullable string, written String?:

var nullable: String? = "abc" // this can be set to null
nullable = null // this is allowed

If you want to access some property on nullable object, that would not be safe, and the compiler reports an error. Before accessing the property of a nullable object you can explicitly check if it’s null or not:

if (nullable != null) 

The other option is to use the Safe call operator ?. Safe calls are really useful in chains. Let’s say we have a User object which has an Address. Address has a State object. State has the String value of the state. Now if we want to access the getState() inside the State object we can do user?.address?.state?.name

Such a chain returns null if any of the properties in it is null.

Conclusion

In this article we saw what we mean by null reference and how this is handled in Java vs Kotlin.

One thought on “NULL handling in Java vs Kotlin

  1. yes you are right…Android apps are considered a potent option to drive business growth to a higher tier. But, picking up the best programming language can be a dilemma. Java is still the most popular option, and the programmers appreciate it. However, with the arrival of Kotlin, there is now a widespread debate! In this space, we will discuss the difference between Kotlin and Java in Android and let you form your opinion.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: