2 Functional programming in Kotlin: An overview

In this chapter

  • Declaring and initializing fields and variables
  • Kotlin’s classes and interfaces
  • Kotlin’s two types of collections
  • Functions (and control structures)
  • Handling nulls

In this chapter, I provide a quick overview of the Kotlin language. I assume that you know a bit (at least) of Java, so I stress the differences between the two languages. The intent is not to teach you Kotlin. You’ll find other books for this. If you need an in-depth coverage of Kotlin, I recommend you read Dmitry Jemerov and Svetlana Isakova’s Kotlin in Action (Manning, 2017).

This chapter gives you a first glimpse of what to expect from Kotlin. Don’t try to remember everything. Look at some of the astounding features of the Kotlin language and see how it differs from Java. In the following chapters, I come back to each feature, used in a safe programming context. In the rest of this chapter, I give you an overview of the most important benefits of using Kotlin. This presentation is certainly not exhaustive, and you’ll discover additional benefits in the rest of the book.

2.1 Fields and variables in Kotlin

In Kotlin, fields are declared and initialized using the following syntax:

val name: String = "Mickey"

Note the differences with Java:

2.1.1 Omitting the type to simplify

2.1.2 Using mutable fields

2.1.3 Understanding lazy initialization

2.2 Classes and interfaces in Kotlin

2.2.1 Making the code even more concise

2.2.2 Implementing an interface or extending a class

2.2.3 Instantiating a class

2.2.4 Overloading property constructors

2.2.5 Creating equals and hashCode methods

2.2.6 Destructuring data objects

2.2.7 Implementing static members in Kotlin

2.2.8 Using singletons

2.2.9 Preventing utility class instantiation

2.3 Kotlin doesn’t have primitives

2.4 Kotlin’s two types of collections

2.5 Kotlin’s packages

2.6 Visibility in Kotlin

2.7 Functions in Kotlin

2.7.1 Declaring functions

2.7.2 Using local functions

2.7.3 Overriding functions

2.7.4 Using extension functions

2.7.5 Using lambdas

2.8 Nulls in Kotlin

2.8.1 Dealing with nullable types

2.8.2 Elvis and the default value

2.9.3 Using loops



Unfortunatly, we weren't able to load your books.

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes) homepage