Skip to Content
Head First Kotlin
book

Head First Kotlin

by Dawn Griffiths, David Griffiths
February 2019
Beginner
480 pages
11h 48m
English
O'Reilly Media, Inc.
Content preview from Head First Kotlin

Chapter 8. nulls and exceptions: Safe and Sound

image

Everybody wants to write code that’s safe.

And the great news is that Kotlin was designed with code-safety at its heart. We’ll start by showing you how Kotlin’s use of nullable types means that you’ll hardly ever experience a NullPointerException during your entire stay in Kotlinville. You’ll discover how to make safe calls, and how Kotlin’s Elvis operator stops you being all shook up. And when we’re done with nulls, you’ll find out how to throw and catch exceptions like a pro.

How do you remove object references from variables?

As you already know, if you want to define a new Wolf variable and assign a Wolf object reference to it, you can do so using code like this:

var w = Wolf()

The compiler spots that you want to assign a Wolf object to the w variable, so it infers that the variable must have a type of Wolf:

image

Once the compiler knows the variable’s type, it ensures that it can only hold references to Wolf objects, including any Wolf subtypes. So if the variable is defined using var, you can update its value so that it holds a reference to an entirely different Wolf object using, for example:

w = Wolf()
image

But what if you want ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Kotlin in Action

Kotlin in Action

Svetlana Isakova, Dmitry Jemerov
Kotlin in Action, Second Edition

Kotlin in Action, Second Edition

Svetlana Isakova, Sebastian Aigner, Roman Elizarov

Publisher Resources

ISBN: 9781491996683Errata Page