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.

Methods that work with nullable parameters

Methods that work with nullable parameters

- [Lecturer] So let's imagine we don't know what the value of name is when we get to this line of code here, so some other code has happened, maybe it's called a function somewhere else, so all sort of things could have happened to our variable name and we don't know, and our compiler doesn't know, more importantly, right now, whether this is a null or a non-null variable. In order to do a method like to upper case, we need to write some code that will mean there is no chance of getting a null pointer exception. Now there are a few ways we can do this. Let's start with a not very Kotlin-like way, and that is, we could use an if statement. So we could do something like, if name is not equal to null, then, then we can line our line of code. Now, as you'd expect, what's happened here again is a smartcast. Because we've checked name is not equal to null, that means that this code block can only run if name isn't null, so Java can do the smartcast. We've now got a non-nullable string, it's…

Contents