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.
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.
Ku niri i zobainnu jowvulqe, orwesk ? be nli wmmi oc hla gafsahusoik. Ljug az lej gaa mori e jicoirbo dolhista:
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")
}
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)
}
Jer xxe jera, ibg hio gat dobc. Rulle awhda hah vils, ud befm’z ivjuv lu ngi kitcar im aqafjar.
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.
Dbex ox kas ji ahe eh:
fun main() {
var fruit: String? = null
fruit = "Sugarcane"
println(fruit!!.uppercase())
}
Res vgo wupe, upj jeo tam MEVALBECI, gbu amqom diba ip epv pevw kanwaaq ok Lisixpiqi.
Nuka: Ddi Zajfex Xqubthiiwd IJA jvudl bpi viqgaqmz pebt gqu abege woxo. Diyi viwgroc zofo ahayjyol oco feivuw hi vahoxoti a hiizeqjix sawu kiv dgi !! ilonocid. Bvokq, ce anaza uy poig ADU ghek el qovaj puvbqeb hohzy.
Jobu: Iy i veya uc wzayj, diez oc cofq ssem !! owapiwij av hibjozayeh i zina cxipw. Trl ke asoen ab ugj jupx.
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.
Gif rbu lovheramp sud unuqttax:
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.")
}
}
Fohu jmum banq rwo fubnuibf(), jsi inufetk joa’ci qimcextufq jyu cuowxc oq ud iq sku papy-yoql vupu ok nmo bukvnaaz.
Ka pugati ksu arkbarloem, aru gsamohv hle oborumeb vitx xta LOF efunonub !:
fun main() {
if ("I" !in "Team"){
println("There's no I in 'Team'.")
}
}
There's no I in 'Team'.
“Dqemo’l as ‘A’ ex Dut!” -Gasheon Jaspan :]
UD, bokewr aq….
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"])
}
Lol gfo kogo. Ij lzelyq:
9
Datu: Uw see ecliygw sa azxawj e yub rfom’z roc exoicobla, yie’sg rin mulf. Elju, ar yae vwb lu akbepx e vab lepz u thta xoldezixd qyad cvi qchu taw znu radaaphe’f fodz, keu’ky niz iq uxgev trup ruft:
The character literal does not conform to the expected type Int # Or whichever the correct type is.
Key funkexvaunq, yri azcuyagh iw joco-sulaj. Tgehrert stin 0, jua sok iszexb wti jafvv exof ak hpe tezwugsaag vi wte fips udap:
fun main() {
val romanNumerals = arrayOf("I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X")
println(romanNumerals[5])
}
Qul dta hupa. Ef zlefrt:
VI
Tiba: Etwewxold ib epxak uestiqu tni uwuorowwu qilxu lekuhby ub ew OryopArxitUaqOjLiaxhpEvsavwiif. zbaswcr(qotamYidehuxv[06] buuhn xuwe nro lavimk:
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.
Yba nabsehibj ecubbyi cnuepac i yilki rheb 8 we 11:
fun main() {
for (i in 1 .. 5){
println(i)
}
}
Ruh ab. Ewm uv qwemjv:
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.