Kotlin 1.4 Coming with NPE Optimization
- By John K. Waters
The Kotlin community released the 1.3.5 update of the language earlier this month, and along with it, plans for the upcoming 1.4 release and an explanation of a potential impact on Java developers.
The plan is to correct an issue around NullPointerExceptions (NPEs) that stems from Kotlin's interoperability with Java code. Kotlin decreases the chances of NPEs by providing support for nullable types. But because of the language's interoperability with Java, it's impossible to avoid them completely. Kotlin compilers are designed to throws different types of runtime exceptions with clear error messages instead of pure NPEs, which helps developers better understand the source of a nullability problem, but reduces the potential null check optimizations that might be performed by the Kotlin compiler or other bytecode processing tools.
To solve this problem, explained Svetlana Isakova in a blog post, starting with Kotlin 1.4, all runtime null checks will throw a java.lang.NullPointerException instead of a KotlinNullPointerException, IllegalStateException, IllegalArgumentException, or TypeCastException.
This applies to the !! operator, parameter null checks in the method preamble, platform-typed expression null checks, and the as operator with a non-null type. It does not apply to lateinit null checks and explicit library function calls, such as checkNotNull or requireNotNull, Isakova said.
Isakova, who is a developer advocate at JetBrains and co-author of "Kotlin in Action," added that, from a developer's perspective, things won't change that much. "The Kotlin code will throw exceptions with the same error messages as before," she said. "The type of exception changes, but the information passed stays the same." She provides some useful examples in her post.
"After this change of behavior," she added, "the optimizers will be able to decrease the total number of null-checks present in the bytecode by removing repetitive null-checks when possible; since all checks throw the same NPE exception, only one can remain. During such optimizations, the specific helpful messages of NPEs can be lost, but that's the price to be paid for the gained performance benefits. Note that these optimizations are still to be implemented in the corresponding tools, and when implemented, there will be more details about that, but the change of the exception type makes it possible in the future."
A NullPointerException is thrown when an application attempts to use an object reference that has been assigned the null value. This includes things like calling an instance method on the object referred by a null reference and accessing or modifying an instance field of the object referred by a null reference.
John K. Waters is the editor in chief of a number of Converge360.com sites, with a focus on high-end development, AI and future tech. He's been writing about cutting-edge technologies and culture of Silicon Valley for more than two decades, and he's written more than a dozen books. He also co-scripted the documentary film Silicon Valley: A 100 Year Renaissance, which aired on PBS. He can be reached at [email protected].