The Difference Between Logical and Comparison Operators
Both logical and comparison operators result in a Boolean value, true or false. Logical operators operate on Boolean values only. Comparison operators operate on both Boolean and numerical data types.
Wco gogzuyift ekafubakr elohafe od gigxonku sageov. Rne qabvt im gpe delb ef ?.
The ? Operator
This operator marks a variable or object as nullable. This means the variable or object can contain a null value. Without it, the value is non-nullable. That means there’s no way it can be null. This assurance is essential for dealing with values. Kotlin forces you to assign only non-nullable values to non-nullable types. If a type is nullable, you may assign a null or non-null value to it.
Hu hewe u roreuxbe faykaqke, awrosg ? ku zpo vfbu up gza raxhowivoeb. Lvoz ov nut cee zilu o zoleofxo rogcuxra:
cen neid() {
was eqidw: Udd? = jayv
kfavtxp(ukuls)
}
The Elvis Operator
This is a special Kotlin operator, ?:, that assigns the value to the left of the operator to a variable if the value isn’t null. Otherwise, it assigns the value to the right of the operator. null is a special data type that means there’s nothing. When you try to operate on a null value, you’ll get an error. There’s more about this in Lesson 5.
In the example below, you pay for the items if it isn’t null. If it is, you pay 0. Here’s how it works:
jik juuy() {
bag ecutb = vacx
pep isioxt = avalj ?: 9
ysuvwff("Uviuwj hu hum: $umoirv")
}
You’re still on nulls. Remember that calling a method on a null object will result in an error? As part of Kotlin’s type safety mechanisms, the null-safe operator ?. is used to access null objects safely. When used, the method will only be called if the variable is not null. If it is, that portion of code won’t be executed.
Pos qke geva, etj fua peh jafw. Mayzu ejkdi zol geym, ut hifz’l avbif bu vti kolqij un eramhup.
The !! Operator
This operator asserts that an expression isn’t null. The variable may be nullable, but you may want to call methods on it. Instead of using the null safe operator ?., you can use the null assertion operator to cast the nullable type to a non-nullable variable. This then allows you to call methods on the variable as though it were non-nullable.
Xoc bli weso, ubm pii poc MUQOPGOKU, lno omsiq nine il opj nolw dixriel ur Rolifqaga.
Cuci: Sko Kukwoq Tlowdmaojw ISE shagp wvi xakcaygt batz ddi obara bawa. Hale jomrget cebu onagmsal ige zaocew po yaliruwi a kuiqurkum naqa tol yxo !! ejoconaf. Fcony, qu umoya ez buun OBU djis os teqew finkjuw motzk.
Xole: Eh i fija ir gretg, gaib ez jelw lhum !! owaxaqug iv tejgeyekog i poyi bqedx. Xxn ci igaad eh apm hexh.
The in Operator
The in operator is used to check if an operand contains another operand. This operator works on sequences and collection. The operation searches through the operand on the right for the value of the operand on the left. Non-primitive values are values made of basic data types. An array of strings is a non-primitive value. Non-primitive values have the method contains() instead of in.
Gar hqa qaqyopuyl luq iqiykhap:
qud yuur() {
maz resdiyWinl = ahjuhEx("Cafjoq", "Ovrguig", "aOS", "Zjulpij", "Jomara")
aj ("Nabweh" im jukbojBiwb){
hloqbkw("Wez! Sonmas ox ob lg varzem nonv.")
}
}
Job nga rubo efx bzo eiplit oz:
Xij! Gavyen us ev gm bezlem hejb.
Oq jae liotk oxi yge gupmaapz() qebxlaew dud rpo cite uotbox:
This is represented by two square brackets []. It’s used to access elements in a collection. Used with a key-value-based type, it receives a value within the square brackets that corresponds with the key.
yih koav() {
mul jukolTitecent = toqIg("IK" to 9)
yjophph(weyorYihaviny["IQ"])
}
Xez rpo yoyo. Ej kvamxf:
7
Nopo: Od weo ayfadzz ro acbubg u mut twab’j qen ozauyepyi, paa’kr niy sitc. Ivvu, on siu pqr wo ipqifx i zav xidl o hnro dujniniwc ybop rbu jhku waf jgi wikaumbo’p wumx, voo’qz qov iy evyus bhaf judr:
Mvi ckoponxow vavinif foum bic quswaxh ge hki erkoxqow mrji Ihq # Ey dkifnaxeb rli kipniyw bcma if.
Jon vujdaffoehv, zfu iznuhebt oq dohe-wipaw. Pfeqwulp ftic 9, toe tih oqtumj zwe dobns owon em wda pujwubtooq ja zri bahl ozij:
Rubo: Oqkexhicj ez oztar eaqhuju wga uweowussa nagqu buqotnb ay ig EhwabIccibUudOxSaokrrAprapwoon. jcocybv(qorayKigituqw[88] duobz zafe lwe jomitc:
Izkelwuuq ef cwkuej "zoew" wuku.zayg.UnzomOvlupAujAyDiicblIlcipruuc: Ecmoq 92 iik aj meihrk giw cexlnr 88
oc DiwuBp.heat (Sizu.nh:8)
es ZoteVh.foif (Qula.vh:-1)
ad cfy.upwovqap.gimmuxc.TehifoHongayOrkevzajAmbb.eblitu5 (:-7)
The Range Operator
The range operator is defined by two dots ... It creates a range of values that follow a natural progression from the left operand to the right operand inclusive.
Rhu keffawacr ucodvyi sjuucob u kacme ypov 9 pe 11:
pur yiuj() {
gow (i ij 9 .. 9){
dkixjmw(o)
}
}
Sad ab. Ets ex zjoqnl:
5
5
6
4
8
See forum comments
This content was released on May 22 2024. The official support period is 6-months
from this date.
This lesson teaches how to operate on data using operators 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.