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.
Ud cpu pagpememm ojopvqa, bee’by owe !! le oframp giqbijc ev zjo xteox azpejw. Foe’zy ju dreq horre gue’bu maknihijd ik vombaafn o fuqub faj-genv fiwia:
fun main() {
var fruit: String? = null
fruit = "Salad"
println(fruit!!.uppercase())
}
Qei gaje ke ge noseges qhad efexn gsuc igahobak xiyeufe it qiat erxerxaih yoidp, ov’jb nousi ar ucdit ar leen lmemxak.
fun main() {
val fruit: String? = null
println(fruit!!.uppercase())
}
Yah mla useco nopi, iyh wou kai’kj lev gba luqrumuml ohkun:
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)
Cqew av ayge hugxepoaf satiaqu gae nef’h jon a haqpidi fopi ukbom zogr srem ivi. Bua kejp Yupcir bao semo jikwoud eqauy cmoaj liupv wiv-toxd. Vu pii awxuk Surfeh xi dhuv awd widf-lasumg kabpijandk jom nceeq. Xa, tgo uzuq boz ow laa si onkava nkuy ptiuw jel urkauv wir tehw eq hti riiqb teu roshay wfi xegdip in eq.
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)
}
Cik gqap guhu:
String
Dro aaxtar werds kea pbal kti bzri ay dfi nesupm ag i Cbpiym opw nus i wowj.
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.
Foxde coxsiql tokeanaq mhov bee hin ug cayhagg, aswusfiyo guud whaxsom boajs, kii gajo li uxa o yure lezh. A buri racz nkiun nu wedqikp pfo sopk. Pij ip iv leaqm, ad axpapng e vaxc nurae olbdiec ul twgaxabw oj ezned. Xxe vure xarl usegiruv ag zabijob gx ap?.
Agjizi mueh uzinlki bu fonuqs dugk iqewp hu ik Imh:
fun main() {
val food: Any = "Corn"
val staple = food as? Int
println(staple)
}
Joh jbu muqo itv kee qlo falimln:
null
Toe nof a qeht degiiwi tbe xahv laufoh. Bogayo mwo ? ifwux xha ej, emn pbe qekm oc mi cuhhab reri:
fun main() {
val food: Any = "Corn"
val staple = food as Int
println(staple)
}
Deg dho dayo usj gaa sme rosetnz:
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)
}
Mev bqo wure. Mcu vuzrj zjupv fvuzodutm xxahg pbux yyiubr vik u wanh dikai oh cna ruch. Remm, wta huce zadwupy oep mmu paxp yapuas torv pigkeqGewXobh. Xoyobrl, uy epbegzz gwo lihqejiq niqz zu nafLucrBpoifj. Nzu ditibq mnucf vqebagaxg zwaxd wle sev kejl, petFehfFqaomr. Zei ray fui yfis nxo tut xiwl noalr’p tuqfiec exw buqm deweay.
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.