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

Special case of building multi-arch container images: Distroless, Go and Podman

CS373 Fall 2021: Erica Xu: Final Entry

The Ultimate Guide to Getting a Developer Job in Europe v.2021–04

Paxos from Scratch

Tips on How to Effectively Learn How to Code Online

Coding clipart

Ananth’s LIFESMART Dashboard

C# conprogram to convert currency to words using indian standard

Number to Word

7 Most Crucial IntelliJ IDEA Keyboard Shortcuts You Should Know by Heart

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

Cancelling Kotlin Coroutines

How can we write tests for Coroutine Flows and Stateflow in Kotlin

Pair and Triple — Kotlin

Null Safety in Kotlin