About This Series
This “Android Kotlin Basics” blog series is all about fundamentals. We’ll take a look at the basics of building Android apps with Kotlin from the SUPER basics, to the standard basics, to the not-so-basics. We’ll also be drawing comparisons to how things are done in Kotlin vs. Java and some other programming languages to build Android apps (like C# and Xamarin or JavaScript/TypeScript for Hybrid implementations).
Check Out the Pluralsight Course!
If you like this series, be sure to check out my course on Pluralsight – Building Android Apps with Kotlin: Getting Started where you can learn more while building your own real-world application in Kotlin along the way. You can also join the conversation and test your knowledge throughout the course with learning checks through each module!
Watch it here: https://app.pluralsight.com/library/courses/building-android-apps-kotlin-getting-started/table-of-contents
Kotlin Null Safety
The NullReferenceException
and NullPointerException
… The bane of many of our lives as developers… say “goodbye” in Kotlin!
Kotlin made the early decision to alleviate the pain of null checks in our code.
In Kotlin, your properties, return types, local variables or values, and everything else needs to be explicitly nullable – not just primitive types, but complex types like your own classes and interfaces too!
Here’s what I mean:
class Dog() { val name: String // this won't compile! name is NOT a nullable string, // so it MUST be initialized! } ... class Dog() { val name: String = null // this won't compile! name is NOT a nullable string, // so it MUST be initialized with a non-null value! } ... class Dog() { val name: String? = null // This works! name is a nullable string and is initialized with // null } ... class Dog() { val name: String = "a name" // This works because the type is a non-nullable string // and is initialized with a string literal }
Okay, so now we know that variables and values must be initialized with either an explicit nullable type or a non-nullable value, but what about if we have a nullable type, and try to do something with it? Let’s take a look!
val dog: Dog? = null println(dog.name) // This won't compile! dog is a nullable type, so you need // access the name property either dangerously or safely with // null coalescing ... val dog: Dog? = null println(dog?.name) // This works! it will print null println(dog!!.name) // This will compile, but throw an exception! println(dog?.name?: "a default name") // this works! it will print "a default name"
Kotlin takes null coalescing to the next level by allowing you to provide a default value if anywhere along the chain is null. This is what we do in the above example – this means that if dog
or dog.name
is null, we use the default provided value notated with the ?:
notation.
This also works for invoking functions with null safety!
class Dog() { fun bark() { println("WOOF!") } } ... val dog: Dog? = null dog?.bark() // this works! but nothing will happen since dog is null ... val dog: Dog? = Dog() dog?.bark() // this works and prints WOOF!
Those are the basics of handling your nulls in Kotlin! No more ugly code like this:
if(dog != null && dog.name != null) { println(dog.name); }
Shorter – Simpler – Easier to read and follow. Once again, using Kotlin makes our lives so much easier while building our apps.
Coming up in future posts, we’ll start looking outside just the basics of the language, and start implementing them with Android common situations!
If you like what you see, don’t forget to follow me on twitter @Suave_Pirate, check out my GitHub, and subscribe to my blog to learn more mobile developer tips and tricks!
Interested in sponsoring developer content? Message @Suave_Pirate on twitter for details.