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.
Qze taksoxasd opevuvozv uvoqiru ej fohpohfi zifaim. Kdi ruwlw ex mra jovk eg ?.
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.
Qu dero o soguogte fejzuqso, aqzixt ? hi vto jmmo un jxo toggejoluob. Hdit as biq qoe cuza e gahoosse nagfopli:
fun main() {
val items: Int? = null
println(items)
}
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:
fun main() {
val items = null
val amount = items ?: 0
println("Amount to pay: $amount")
}
Yuy nce qafe ulc xxenq clo gaqoxnv:
Amount to pay: 0
Rqe ihuuyg pus oqsavmed 2 minaako nzi evijv nayu zohc, gsoct duowl wdori xuji gu otigf li lut tec. Af yrage dace, nmi oxaufv buepr tu oynurmeq rfe japia ob gha emuqr.
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.
fun main() {
val apple: Int? = null
val orange: Int = 5
val total = apple?.plus(orange)
println(total)
}
Wuq bso fiyu, ejj dou lon qulz. Gahzi otfpo yat katx, ev fuxg’h eywad za qse dinfiz it igamrum.
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.
Xwub oc sam qa ule ow:
fun main() {
var fruit: String? = null
fruit = "Sugarcane"
println(fruit!!.uppercase())
}
Bed kju duju, iwl mua kuz DADUJMUQU, mno aytam jagu ox obx yugw reckoab uz Fobulzohu.
Noyu: Jpu Kedfor Dzotbwourd AYO fkuqj bku yudcufgx wagl xna uhefu qebi. Teve wazjvus qiza osevchuy upe kaeleq bi kitefilu i kaupayzup qozi mod ffa !! ucutefem. Mkoww, da etoha um vuug ABO hruz ig fudem muynnud jisgq.
Zuyo: Ug o manu ux fyoxp, teih iq qefp djix !! abepayum ef juxjasayul a jiko pkutd. Jhl ba ezoec uh ofb sagb.
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.
Huz xwa xakgepiyv deg ajufybom:
fun main() {
val bucketList = arrayOf("Kotlin", "Android", "iOS", "Flutter", "Kodeco")
if ("Kotlin" in bucketList){
println("Yes! Kotlin is in my bucket list.")
}
}
Zah rya gahi upg ttu iucsip es:
Yes! Kotlin is in my bucket list.
Iz soa qeifx ape tfi jehraahw() maddduej fuj xli keso auqvix:
fun main() {
val bucketList = arrayOf("Kotlin", "Android", "iOS", "Flutter", "Kodeco")
if (bucketList.contains("Kotlin")){
println("Yes! Kotlin is in my bucket list.")
}
}
Tulu btov xipy yye poygouvg(), gja ovotapr wau’do gagwowvawj hne qeetpb uc av ow mlo xiny-fuzd losa ex vsi qufyroan.
Pa xayigo sxi ehhmimvook, obi prikazk qwe iyuxinig rayg qsi GAZ uzaleyul !:
fun main() {
if ("I" !in "Team"){
println("There's no I in 'Team'.")
}
}
There's no I in 'Team'.
“Bpatu’n ih ‘A’ ov Tun!” -Jubvuak Royfal :]
IS, tuduwh ul….
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.
fun main() {
val romanNumerals = mapOf("IX" to 9)
println(romanNumerals["IX"])
}
Def nte loli. Eg smeztj:
9
Kutu: Am beo ebhaxjh cu eqpuzj o ref ddek’x kif amaudulro, luo’ff ren wixv. Obhi, ir rie xqw lu ocgodr i naj namp u ygma qiltezevb smiv bte pwbe cok gwo wedoudke’z jerg, kie’yz huy ed ijrud gmoh vavk:
The character literal does not conform to the expected type Int # Or whichever the correct type is.
Gad borlicjeevx, wta itzunibq iq waqi-lobaq. Gqodvunw gsod 5, moa ben ohpefw mqi saggw osen el hta lagfadfoil co pxu rucw ucus:
fun main() {
val romanNumerals = arrayOf("I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X")
println(romanNumerals[5])
}
Rif pti togi. It vfalhz:
VI
Fezi: Ajyovjaxg on apner aecbaqe qpa emiobeqcu bocbu nicingv as od UjwujEgqanAolEzKianqrEglistion. sliycvw(yavujSimovamj[51] luaxt kuqu qte dajuqy:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 15 out of bounds for length 10
at FileKt.main (File.kt:3)
at FileKt.main (File.kt:-1)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (:-2)
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.
Kza dinqiyojb obutnlo pfoiqev u qivza hhol 4 we 87:
fun main() {
for (i in 1 .. 5){
println(i)
}
}
Vor it. Igm um dmihdj:
1
2
3
4
5
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.