liveBook · Manning

Chapter 7: Working with nullable values

What you will learn in chapter 7 of Kotlin in Action, 2E

Introduces nullable types as a Kotlin feature that improves code reliability and previews the chapter’s treatment of null values and Java interoperability.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces nullable types as a Kotlin feature that improves code reliability and previews the chapter’s treatment of null values and Java interoperability.

7.1 Avoiding NullPointerExceptions and handling the absence of values: Nullability

The section explains why Kotlin makes nullability part of its type system: to catch many potential null-related errors at compile time rather than as runtime exceptions.

Key terms: nullability, type system, NullPointerException, compile-time errors, run-time errors

7.2 Making possibly null variables explicit with nullable types

This section explains how Kotlin marks types as nullable, restricts their use, and allows a null check to make a value usable as non-null.

Key terms: nullable type, non-nullable, String?, question mark, non-nullable type, NullPointerException, null comparison, if checks

7.3 Taking a closer look at the meaning of types

Explains why Java’s treatment of null weakens what a declared type guarantees, and how Kotlin’s nullable types address that problem without wrapper objects.

Key terms: type, String, null, NullPointerException, nullable types, non-nullable types, intrinsic checks, @Nullable, @NotNull, Optional

7.4 Combining null checks and method calls with the safe call operator: ?.

This section explains how Kotlin’s safe-call operator combines a null check with a method or property access, including across several nullable properties.

Key terms: safe-call operator, nullable, ?., safe calls, nullable properties, object graph

7.5 Providing default values in null cases with the Elvis operator: ?:

This section explains how the Elvis operator supplies a fallback for null values or exits a function when a required value is missing.

Key terms: Elvis operator, null-coalescing operator, safe-call operator, return, throw, preconditions

7.6 Safely casting values without throwing exceptions: as?

This section explains how `as?` avoids cast exceptions and how to combine it with the Elvis operator to reject values of the wrong type.

Key terms: as, as?, ClassCastException, safe cast, Elvis operator, smart casts, equals

7.7 Making promises to the compiler with the non-null assertion operator: !!

Explains what Kotlin’s non-null assertion does, when it can be justified, and why it should be used cautiously.

Key terms: non-null assertion, !!, non-nullable type, Elvis operator, isActionEnabled, executeCopyRow, stack trace

7.8 Dealing with nullable expressions: The let function

This section shows how safe-call let handles a nullable expression and compares let with other Kotlin scope functions.

Key terms: let, safe-call operator, nullable type, non-nullable parameter, it, nested let calls, if expression, apply, also, with

7.9 Non-null types without immediate initialization: Late-initialized properties

This section explains how lateinit permits deferred initialization of non-null properties and why it is useful with frameworks that initialize objects after construction.

Key terms: lateinit, non-nullable type, var, !! operator, lateinit properties, Java dependency injection frameworks, field, visibility

7.10 Extending types without the safe-call operator: Extensions for nullable types

This section explains how extensions on nullable types handle null receivers, why calling let without a safe call does not filter out null, and when to define a nullable-receiver extension.

Key terms: extension functions for nullable types, nullable receiver, this, isNullOrBlank, let, safe-call operator, extension function, nullable type, non-nullable type

7.11 Nullability of type parameters

Explains why a generic type parameter can allow null without a question mark and how a non-nullable upper bound prevents that.

Key terms: type parameter, nullable type, Any?, non-nullable upper bound

7.12 Nullability and Java

Explains how Kotlin interprets Java types when nullability annotations are present or absent.

Key terms: @Nullable, @NotNull, String?, platform type

7.12.1 Platform types

Explains how Kotlin handles Java types with unknown nullability, why platform types exist, and what risks developers must manage when using them.

Key terms: platform type, nullable, non-nullable, NullPointerException, platform types, null checks, ArrayList<String?>?, String!, non-nullable Kotlin variable

7.12.2 Inheritance

This section explains how Kotlin implementations of Java methods choose nullability and why non-nullable parameters can cause runtime exceptions when Java passes null.

Key terms: overriding, nullable, non-nullable, non-null assertions

7.13 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 6: Working with collections and sequences Chapter 8: Basic types, collections, and arrays →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.