By using this site, you agree to this use. See details.
Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
All types in Kotlin are non nullables (they don't include the
To do so the type must be explicitly marked as nullable with the
Calling a function on a nullable value might raise a
Thanksfully Kotlin has a safe call operator :
What if we don't want to stay in the dangerous nullable world and escape from it ?
We can thank Elvis for that and use the operator
?: allowing to specify a non-null value to use in case the value is null.
I ❤️ (K)NPE
Finally if really we want to raise a
NullPointerException we can use the
In Kotlin the keyword
as is used to cast an object into a specific type. If the cast fail a
ClassCastException is raised,
as? can be used to return a
null value if the cast failed.
Using the operators
!is we can check if an object is of a given type. What's really nice is that with smart casts the compiler infer the type of our object after these operators.