Created
June 29, 2020 11:15
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Null reference | |
var a: String = "abc" | |
a = null //compilation error | |
//output: Null can not be a value of a non-null type String | |
//Nullable operator ? | |
var a: String? = "abc" | |
a = null //ok | |
print(a) | |
//output: null | |
var a: String? = "abc" | |
a = null //ok | |
print(a.length) //compile error | |
//output: Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type String? | |
//Null-safety operator ?. | |
var a: String? = "abc" | |
a = null | |
print(a?.length) //ok | |
//output: null | |
// Not-null operator !! | |
var a: String? = "abc" | |
a = null | |
print(a!!.length) | |
/*output: Exception in thread "main" kotlin.KotlinNullPointerException | |
at FileKt.main (File.kt:9) | |
at FileKt.main (File.kt:-1) | |
at sun.reflect.NativeMethodAccessorImpl.invoke0 (NativeMethodAccessorImpl.java:-2)*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment