liveBook · Manning

Chapter 8: Basic types, collections, and arrays

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

Introduces the reliability-oriented choices in Kotlin’s type system that shape its basic types and collections.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces the reliability-oriented choices in Kotlin’s type system that shape its basic types and collections.

Key terms: type system, nullability, read-only collections, arrays

8.1 Primitive and other basic types

Introduces Kotlin’s basic types and its distinction from Java’s treatment of primitive types and wrappers.

Key terms: primitive types, wrappers

8.1.1 Representing integers, floating-point numbers, characters, and Booleans with primitive types

The section contrasts Java’s primitive and wrapper types with Kotlin’s unified basic types, then explains how Kotlin represents those types efficiently on the JVM.

Key terms: primitive types, reference types, wrapper types, Int, coerceIn, Java primitive type, Java wrapper type, generic classes, java.lang.Integer

8.1.2 Using the full bit range to represent positive numbers: Unsigned number types

This section explains what Kotlin’s unsigned integer types are for, how their ranges differ from signed types, and how they work on the JVM.

Key terms: unsigned number types, signed counterparts, full bit range, UInt, unsigned integers, regular integers, JVM, unsigned numbers, inline classes, signed counterpart

8.1.3 Nullable primitive types: Int?, Boolean?, and more

This section explains when Kotlin primitive values use boxed JVM representations and how nullability affects operations on them.

Key terms: nullable primitive types, wrapper type, Int?, nullability rules, generic classes, type argument, boxed representation, arrays

8.1.4 Kotlin makes number conversions explicit

Explains why Kotlin requires explicit conversions between numeric variables, how literals are treated, and how strings can be converted to primitive types.

Key terms: numeric conversions, toLong(), toInt(), boxed values, equals, number literals, L suffix, arithmetic operators, overflow, toInt

8.1.5 Any and Any?: The root of the Kotlin type hierarchy

This section explains how Any and Any? sit in Kotlin’s type hierarchy, how Any relates to Java’s Object, and which methods Any provides.

Key terms: Any, Any?, non-nullable, null, automatic boxing, java.lang.Object, platform type, toString, equals, hashCode

8.1.6 The Unit type: Kotlin’s void

Explains how Kotlin’s Unit serves as a no-result return type while remaining a real type with a value.

Key terms: Unit, type argument, return Unit, void, block body

8.1.7 The Nothing type: “This function never returns”

This section explains how Kotlin’s Nothing type marks functions that never return normally and how the compiler uses that fact when analyzing callers.

Key terms: Nothing, return type, Elvis operator, non-null

8.2 Collections and arrays

This introduction connects Kotlin’s collection APIs to Java collections and introduces a closer look at their relationship.

Key terms: Java collections library, extension functions

8.2.1 Collections of nullable values and nullable collections

This section distinguishes nullable collection elements from nullable collections and shows how to process and filter nullable values.

Key terms: List<Int?>, List<Int>?, List<Int?>?, type argument, map, toIntOrNull, Int?, filterNotNull, List<Int>

8.2.2 Read-only and mutable collections

This section explains how Kotlin separates read-only and mutable collection interfaces, how to choose between them, and why a read-only view does not guarantee immutability or thread safety.

Key terms: Collection, MutableCollection, read-only interface, mutable interface, read-only interfaces, mutable variants, defensive copy, read-only view, immutable, thread safe

8.2.3 Kotlin collections and Java collections are deeply related

Explains how Kotlin’s read-only and mutable collection interfaces correspond to Java collections, and why passing collections to Java requires care.

Key terms: Collection, MutableCollection, Map, MutableMap, java.util, setOf(), mapOf(), Set, read-only interfaces, read-only Collection

8.2.4 Collections declared in Java are seen as platform types in Kotlin

Explains how Kotlin treats collections declared in Java and how to choose Kotlin collection types when implementing Java methods.

Key terms: platform types, read only, mutable, List<String>?, MutableList<String?>, contract

8.2.5 Creating arrays of objects and primitive types for interoperability and performance reasons

Explains how to create Kotlin arrays, choose object or primitive arrays for interoperability and performance, and use collection-style operations on them.

Key terms: Array, arrayOf, arrayOfNulls, toTypedArray, vararg, Array<Int>, IntArray, boxing, intArrayOf, toIntArray

8.3 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 7: Working with nullable values Chapter 9: Operator overloading and other conventions →

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.