Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can someone explain me what null is for?

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading