The very common and mostly used method of checking the nullability is by using if-else :) Yes, you guess it right. If it is null then you should show some relevant error message and if it is not null then you can do the operation that you want to perform on that property. So, it becomes necessary to check if the property that you or any other developer is going to use is null or not. Var name: String? = "MindOrks" //no error Sign to the type of the variable as shown below: Var name: String = null //compilation error Name = null //compilation error // other way We can’t use the following syntax in Kotlin: If you are doing so then you will get compile-time error. So, in Kotlin, a normal property can’t hold a null value. If you are familiar with Java, then must have encountered withĪnd to remove this or in other words, we can say that to safely deal with this NullPointerException, the developers of Kotlin introduced Null safety. Null safety is one of the best features of Kotlin.
0 Comments
Leave a Reply. |