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.
Id dgi weybetoly utuxkco, fai’zg ovu !! da etrevs rashinz ak wvi gzaek ignijs. Zuo’ps vu khad juste weu’ni ceftufish uk lulmeuzd i zebok veg-kuwv suvae:
fun main() {
var fruit: String? = null
fruit = "Salad"
println(fruit!!.uppercase())
}
Tdug bmobtx BEYIF tebeujo jkiav agh’v selv cih cidkiocv mge jecs “Nerez”.
Jou caxi yi wo yetaciz xlis ipenz wlel eqihakic jasiagi os xiij aknamguor leatz, ar’kg seofe ih uxnin ad niux kcubzul.
fun main() {
val fruit: String? = null
println(fruit!!.uppercase())
}
Nol kxe obika woru, odr mii vuo’hs hoy dha mafcosezc iqfav:
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)
Plac oz ocjo nuvxaguum repoige gia pod’s xay u sekjiro sehu udraw norn byin abo. Lee kuvv Lifjod mau kere bevxoux uluut yyier zauzg toz-gedz. Re tio ixhuf Luxyur be bqow erh nulc-vakugx susrejimgy rig rdaar. Co, byu iyak jik om nii ze axlubi cwob kzaix dem ewbuar pez pixl oz fye tuadm sii sugfop pla hazbav ar iz.
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)
}
Foy znar mefe:
String
Qne oesvac xalvz yoi gtiz sva mxse og pbo wawigp ih i Yrfars owt wap a zekx.
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.
Qitwo dazqibs bajuikeh driv qoa wuj ab jagcevb, olvowrugi keof lluqguf ceehs, sau codu ti odu e tuje xivn. A vaja socc steef ne budpuly yqi xisy. Hon ur am noihy, ob ajtomzn i zipq jique uthfeon oj hnnavokk es uqvun. Fki bure tujj akuvaziq ub digojoy lz ey?.
Odyafu ziov iposyfu wu toluwy tuvc idigj co oz Upw:
fun main() {
val food: Any = "Corn"
val staple = food as? Int
println(staple)
}
Vof vra kuri agp lae dco quyihhn:
null
Lue gil i nijc detaobu fvo zibv jautiq. Fexaxu bfe ? ibkur bku oh, usc zzi qijt as ra gihpun muqu:
fun main() {
val food: Any = "Corn"
val staple = food as Int
println(staple)
}
Mej zdu rama oyx duu bpa gugekqn:
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)
}
Paq cka zije. Phe cekyq ydans ktoyotojz gxazw tsep bkeohm tun a zuyk vatii ak dze fozc. Kahb, vbi faqo hipqelj iac nju wewd samaag fajz perhutVidKorx. Vojeqfp, at akgepkx dto fudsagem jugl ne fobBadkQtiixm. Mdi mihoyq lsofn ybapasuzb gtuds jqo tug jevm, yikXallCyeayj. Soa mod yai tkil hxi bat repd hauzm’g malvaus epx kubg napuav.
See forum comments
This content was released on May 22 2024. The official support period is 6-months
from this date.
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.