![]() Let’s look at the different ways how we can handle null references safely in Kotlin. Otherwise, it will throw a compilation error since Kotlin knows that the variable contains null references: val a : String = country.length val b : String = city.length //compilation error However, in the case of nullable variables, we need to handle the null case explicitly. We can call a method or access a property on a non-nullable variable. To define a nullable variable, we must append a question mark(?) to the type declaration: var city: String? = "Kolkata" city = null We cannot assign a null value to a variable because it’ll throw a compilation error: var country: String = "India" country = null //compilation error In Kotlin, all variables are non-nullable by default. It distinguishes between nullable and non-nullable references as a part of its type system. ![]() Kotlin aims at eliminating the risk of NullPointerException. ![]() It has been referred to as a billion-dollar mistake. In this article, we’ll look into the null safety in Kotlin.Īny programming language which has the concept of null reference throws a NullPointerException.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |