From the course: Build Spring Boot Apps with the Kotlin Programming Language

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

Creating variables that can contain a null value

Creating variables that can contain a null value

- [Instructor] Hello and welcome to Chapter Four. This is going to be quite a short chapter, but it's certainly an important one and in this chapter we're going to talk about nullable variables. In the first chapter of this course, I said that one of the key selling points of Kotlin is that you won't risk getting null pointer exceptions in your code anymore. In this chapter, we'll learn why. First of all, by default variables cannot contain a null value. Let's create new file to work in for this chapter, and I think we'll call this Exploring Nulls. And again let's just have a main method by typing in the word main and pressing enter. So the following line of code then won't compile. We can't do our name as a string, for example, and set it equal to null. By default, variables cannot contain the value null. And in fact if we hover over the word null it will say that null can't be a value of a non-null type string. So when you declare an object with an explicit type, like we have done…

Contents