Safe calls(?.) vs Null checks(!!) in Kotlin

This post was originally posted at and reposted on Medium on 14th Nov 2017.
var variable : CustomClass = CustomClass()
variable = null //compilation error
var nullableVariable : CustomClass? = CustomClass()
nullableVariable = null //works fine
variable.someMethodCall() //works fine as the compiler is sure that
//the variable can’t be null
nullableVariable.someMethodCall() //will highlight compilation error
//as compiler is not sure as
//nullVariable can be null.
if ( null != nullableVariable) {
} else {
// fallback flow
Smart cast to ‘CustomClass’ is impossible, because ‘nullableVariable’ is a mutable property that could have been changed by this time.

Please continue reading at




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Static libraries in C.

Microservices Interview Questions

PowerShell File Trust without digital certificates

New in MicroStation CONNECT Edition Update 16 — Ability to Specify Grid Shift Files in your custom…

Many-to-Many Relationship and Simple Calendar Rails Gem

Google destroyed my Google Play Developer Account and my work for the last 2 years.

Developer Tip to Save Your Eyes

Executing a Python Script From a Java File and Vise-Versa

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Suneet Agrawal

Suneet Agrawal

More from Medium

Null Safety in Kotlin

Kotlin When Expression

Primer for String on Java/Kotlin

Working with enum constants