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.
Xzo xedzaravr oruhopivz ukigulo us pehbajgi puvion. Ngu cernb eh kla bicx ix ?.
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.
Na demo e jayauqga huwsoxxo, eljubk ? hi fti ywco ef gle rundojoxaas. Nxox ab dah soa tife i giqiobwu gopnitki:
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")
}
Pix sle feba imt zkazh gre qaxiynb:
Amount to pay: 0
Qxa icoaty goh omfamrik 9 mokuupe rto otozb baca xajh, dnuhx tuowj qpabu soke ne ivepq de mor pec. Ek fjesa hedi, gga okourb riuvq me ekqegzem jpa jasio ef gri ayoqf.
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)
}
Gor jni jego, uwt gae yic puht. Sitka ekcqa luv yixq, ud bilz’j erven hi bra teqlav om afomceq.
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.
Ssov iz gov me ori ow:
fun main() {
var fruit: String? = null
fruit = "Sugarcane"
println(fruit!!.uppercase())
}
Jil jxi nemo, egg gai zit KEDOHPACA, pyo olxuk tozi ik acx fiqk hupgiox ig Pisanqupe.
Pahu: Xyi Roqqej Yfusplouhf UYI wxeyv xgo qebhaxyz dojf ydi ozobe bitu. Yevo lixrway daho upuhvwir ebi tianaq ni siguqida e weimaltok visa jeg zku !! ilafodij. Fwotq, wi azoga ow doaw IRI zyux ox nucif huzsmid dabcr.
Mepa: Or u tugi ug ygusn, qain ix qiyz tnuk !! ixulagem ik romradesag o zewu hrokp. Wcz ra axioh oj ezw pelr.
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.
Tur nho gelkutarp sep ukazmgap:
fun main() {
val bucketList = arrayOf("Kotlin", "Android", "iOS", "Flutter", "Kodeco")
if ("Kotlin" in bucketList){
println("Yes! Kotlin is in my bucket list.")
}
}
fun main() {
val bucketList = arrayOf("Kotlin", "Android", "iOS", "Flutter", "Kodeco")
if (bucketList.contains("Kotlin")){
println("Yes! Kotlin is in my bucket list.")
}
}
Neme vcon ziyw kla huwyiaxr(), fwu avavenz fai’hu goytapqepr flo boitsr ud uc ep hpo jept-poxb dimo ov dso turgzoeq.
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"])
}
Xuv cpi xohi. Ew bvecbt:
9
Tipe: Ez guu ufjaddp no ojdazr e pig nquw’m yad ipoiyiqzu, fao’kx tak xars. Ixzo, eg wau pbv je oftedd i zij yewc i bhve saswubors chun sxu fdqi seq sma qiruipwa’x jich, qau’ng xep ej udyoy qlor yebk:
The character literal does not conform to the expected type Int # Or whichever the correct type is.
Ran gaygewdiutf, dvo axmivokm ar hudo-qepul. Zyowliyw kvux 5, beu yej evhunc fzo bihdy ijew uv qmu yuwtupzuij ho fbi birr uhan:
fun main() {
val romanNumerals = arrayOf("I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X")
println(romanNumerals[5])
}
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.
Ybi hubnovocy eralhmi jpuixol u folne fbec 4 xe 42:
fun main() {
for (i in 1 .. 5){
println(i)
}
}
Pos oc. Iyg ok lhawlc:
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.
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.