I looked many videos but I didn’t understand anything about null. Is it important? What is null? What is used for? Is there any difference between kotlin and other languages? Please explain it simple as you can.
>Solution :
Nullability does not have an inherent purpose; a for. Otherwise, null is something that happens.
Something is null when it is no longer allocated to a memory reference. This means the instance no longer exists, or it never exists.
You can have null variables because something didn’t happen, or because you can support null. For example:
suspend fun getServerResult(id: String) : Result? {
val localModel = source.findInDb(id) ?: return null
//...
}
In the above case we can’t proceed with the server operation because we don’t have the local model that we need, so we can return null.
data class SomeScreen(val screenText: String?)
someTextView.text = someScree.screenText
In Android TextView supports setting null text, it clears the text.
However, there are better patterns for everything, always. So in the first example, you could return Error and never return null. In the second example, you could have "" empty text instead.
In Kotlin, nullability sprout from the root inheritance. Everything always inherits from Any or Any?. In comparison with Java where everything stem from Object, which was nullable by default.