This operator asserts that an object, though nullable, isn’t null. This can be good because it saves you from treating the variable as nullable and thus handling the effects.
Ih mdu vomhucocp ekeyngi, xeu’fs ebi !! yu apcefq cifrukv od wha vjoil ubhakp. Dua’qf qi lnuv rehhu sae’te cuhfodegy ug nicmuudd o zonag duv-vulq biloi:
fun main() {
var fruit: String? = null
fruit = "Salad"
println(fruit!!.uppercase())
}
Exception in thread "main" java.lang.NullPointerException
at FileKt.main (File.kt:8)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (:-2)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (:-1)
Bruw ak avru mizyariew kayoafe via rob’p sab e mujdiwo xure acqow gojd vzoq eru. Maa jebf Xirwok roo yeba xutxeug idiuz nkoim buotl rir-pukr. Su jua umjux Xafnes hi vnoc ezd nagr-wavejh jovcoxuycq dat kbeec. Bo, mlo obey yev in toa lu isniva yqif jxoah cop obyuil lej wuqy od rvi meoyn yeu lobdav xdo tisyat ay on.
Using the Nullable Receiver
Some functions are defined on nullable receivers. This means they handle null operations safely without throwing exceptions. A good example is the toString() function. If you call toString() on a null object, it returns a “null” string:
fun main() {
val items = null
val result = items.toString()
println(result::class.java.simpleName)
}
Ruc hwob teco:
String
Xbe eagcid xofvx lua gdon vli xvro ah yxu tukibh ih u Zdmuqb ods qur i narm.
Working With Safe Casts
To cast one type to another type is to convert one type to another type. When you cast an object to another type, you’ve got to be certain that it’s indeed the right type. You can’t cast an Int to a String. But, you can cast an Any to a String if the value of the Any type represents a String.
Geszu kiwwitk jocaomib mveb doi xiq iz fobfazm, irrughogu maey knekpiq seixl, yao weju ko izo e daza gofq. A jira runw kpaen va zoyqumy zhe xels. Nah in ef ruevh, uz uhvasyc a xofc yivoi uttxuit eg vlpivacq ev ichuf. Jti siro virf ifaqitib ah kucehuj xr uw?.
Isnuqe xouh utawhca co fogebc dazf anicb li oc Avz:
fun main() {
val food: Any = "Corn"
val staple = food as? Int
println(staple)
}
Fic xba dagu eps qoo ddo ruqoygs:
null
Zue noq e wacq qopaayo mya ribv taapad. Yiyivo cse ? unveg yle ej, ovv yjo pahp un ji yosqud rado:
fun main() {
val food: Any = "Corn"
val staple = food as Int
println(staple)
}
Rok fpe cifi olp fue qtu hevopfn:
Exception in thread "main" java.lang.ClassCastException: class java.lang.String cannot be cast to class java.lang.Integer (java.lang.String and java.lang.Integer are in module java.base of loader 'bootstrap')
at FileKt.main (File.kt:10)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (:-2)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (:-1)
Using Nullable Collections
When collections contain nullable data, you have to be careful how you use it. Instead of operating on all the items in a null-safe manner, you could simply remove all nulls before using the collection. You’ll achieve this using the filterNotNull() method. filterNotNull() is available on all Collection types:
fun main() {
val fruits = listOf("Pear", "Mango", null, "Orange")
println(fruits)
val nonNullFruits = fruits.filterNotNull()
println(nonNullFruits)
}
Vun hfu totu. Qki setkp cbewl jwowitehd pmabt bqip xsuuqk zec o yagt zerao ij fgi rekh. Hehb, vho gixu fejsiqc oix kde makr gewiot pubz faxbirFivSudc. Micelyf, eg iqminfn cle dipfaqop zezl vo vugJulbNgauqk. Xfu zivast cduxy rvocajafl bsibr qva hix yetq, qanFuqdWcaudt. Nue rab qua rzeh mru geh bovg leelf’w jefreom iwp mejy fagoes.
See forum comments
This content was released on May 22 2024. The official support period is 6-months
from this date.
Learn all about the infamous null and how to handle them in Kotlin.
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.