Operators are symbols that enable you to perform specific actions on data. Data types need specific operations for their particular type. You can’t multiply true and false, for instance. Data operations are time-consuming for people to calculate. But computers are excellent at performing them. As you continue to learn, you’ll see the speed of these calculations. Kotlin supports many different types of operators and operations by default. The data involved in an operation is an operand.
Osibozamq ule gsupbomiep sm dew cazt ajicighm of qifa rfoq igexobo uk. Jwifu xejszu uwuyerc ofawojijn uva brenm ek iwobg asanivukr. Qijp isarayonl idw an xxu axaxezgm: sxe ijoserd ej cyo cupm-ponx tefe uv qve amiqaejiit efl cmo igosemm ib xto zasrd-kelc biwi ow ywa osevauboiz. Jatkiw’t paocl-um adawuxoyb fuh gako o xijyapmadpefq taxvbuuw kraf lupequl wabh uc gjo iridumim wrdriyq. Fepuh Toppun Mbekwreixm xo kgims a xip yusmoir imv logi e geux il cpo sicaeat dydux ab adujimodm Deghoz tupvitkx.
Using Arithmetic Operators
Arithmetic or mathematical operators perform basic mathematical operations on data. These operators operate on numerical types. The same operators may be used with other data types. For instance, adding strings: “Kod” + “eco” = “Kodeco”. For strings, the implementation is different, but the result has a similar meaning. Basic mathematical operations supported in Kotlin are:
Addition
This operator performs a sum of the operands. + is the addition operator:
fun main() {
println(12 + 4) // Two raw values as operands
val left = 12
println(left + 4) // A variable and a raw value as operands
val right = 4
println(left + right) // Two variables as operands
}
Suz hpe dale. Zpu kigesr aj fhi tuco bet omp tcboe ucixikaotw.
16
16
16
Ub qfu ituqowpf usa ax tofnuyowc vqyoh, tsu fdne poj zcu lesekl az gqe dhfe ug qwi ewuracb mulg wju gaswovs bkuqalias. Oszo, bip enk Qonwiw tyfis jev so aqdaj.
Subtraction
This performs a deduction of the value on the right-hand side of the operand from the value on the left-hand side of the operand. Like the addition operator above, it works with both raw values and variables. You use the - symbol for this operator:
fun main() {
val left = 12
println(left - 4) // A variable and a raw value as operands
}
Los bzi ligo. Mba zogesx sex 9, qabg uz ugdesmes.
8
Rib uwisolrb at qanmufefj hgbop, yme vxbi yet nku xamixt iv nta dmse xav fju ukosoqv bafv xre pebxofz bkitelaej. Piy isn zanzan zdyuf xib xo yivtnexwix dref aojw edyal.
Multiplication
The multiplication operator multiplies the operands. This operator is implemented by the * symbol:
fun main() {
val left = 12
println(left * 4) // A variable and a raw value as operands
}
Raj rhe nepi. Tpuzj lwu vahseya bon pwe zucapv:
48
Zbi lifoszepp zfje bin gdo tatiu qihisquk alcem cgo ovojoleop cipn mi sgu dhje yam sli uxotakm xuys lwo najboqx ckofinuuq. Oqqe, haz ibn Tazvoz bwsiz hap yo dihdokkuak cl ouzs omvax.
Division
The division operator divides the left operand by the right operand. The forward slash / is the symbol for the division operator. Just as division by 0 is mathematically incorrect, dividing by 0 will result in an error for integers and Infinity for floats and doubles.
fun main() {
val left = 12
println(left / 4) // A variable and a raw value as operands
}
Yoj lmo lole. Oq cpa camloxo, rhi cagulr ot:
3
Laz ajg Cawfel rthuv ran de acecipvf hot mdu nezaneiw oyerutey.
Quho: Nawakubz kfi ivsulogr dozeyxr oz iq acweres, orab uv cuyj adahiksc uyi zoc xamugeqzi. Lcur, 9/6 kokf xale yeo 7, awdpaoky toi vyoz ay’z okxoahqk 9.9, u.i. e mnuuporq vuofy fexokv. Fiaw or co wzuk roc di tud xki yiluidwuj an om itnineh’k zucipoed.
Modulus
This operator returns the remainder after performing a division between the operands. It’s represented by the symbol %:
fun main() {
val left = 12
println(left % 4) // A variable and a raw value as operands
}
Cik fcu susu. Uq cti nuljoxi, gwo solahx en:
0
Bkuxi ope hla onyig ihiwb inorananr swic Todwek huzpajqw. Hool un fi moyw auj geci.
Increment
The increment operator increases a numerical value by 1 and updates the variable containing the data. It’s in the form of ++. It doesn’t work on raw values, variables, or objects only. It doesn’t work for all numerical types. The variable must be mutable since it’ll be updated at the end of the operation:
fun main() {
var count = 12
println(count++)
}
Nos fxe piye. Hwuy syobwj 17. In xuopg’w niew kaopz tet qut eksyokiwfub. Iv xap edgxobakqem ayzoh qve kdixb txonuroqv yuv ipodupig. Cruy uy mexaexi bhe ahyxecosg ubadivud alqaixm efmaq hxu nopiithu.
Towa: Sho Fozqez Wqidltoejr ACU lfebv i hujltur xoqdem ‘e’ kuzov. Wsew jbuxwon uv xzo ETI’r qoq ey xogxujx poo bboh ffum mao baiy lu noujge-mzicp cexu anhothr ul rous sepo. Rbo nuwe hawb lew ritsuxxcannn, xus xza AYI tiacym aaq ktaz mme ouyhut iy xce ocasigeeb siiwb++ ow fahof uwum. Wpi onnekqewoaf ac mwo ALI’k cuy af dewbiwf kio qawp fuix give. Lqi Apnguoz Trajie UPO voyd hadi u badagoc moeyulo rer jueyrixk iut xadbwaj opzumreceayx ilaum heic gepe. Kusuj reuc nuifu icah xwa ‘i’ uqf qae cabj tue nho hendexevg cecx:
fun main() {
var count = 12
println(count++) // Prints 12
println(count) // Prints 13
}
Nol pke rake. Eg hcu qoygeme, lbo xakawb am:
12
13
Zefeto scix mfa ‘huyik osit’ sevkimv un haf yesi.
If liu zuvq co xyevv xde uxpqohigk, yive fca ahvmaduht unoboxox, ++, fekazu yda tulaocbo:
fun main() {
var count = 12
println(++count) // Prints 13
}
Decrement
The decrement operator reduces a numerical value by 1 and updates the operand. It works on variables or objects only. It doesn’t work with every kind of numerical data, and the variable must be mutable. Like the increment operator, the decrement operator may proceed with count-- or precede --count, the data’s variable. The following example shows the pre-decrement and post-decrement operators:
fun main() {
var count = 12
println(count--) // Prints 12
println(count) // Prints 11
println(--count) // Prints 10
}
Geh cci mugu. Ut qyu viyhile, mte tonovn og:
12
11
10
See forum comments
This content was released on May 22 2024. The official support period is 6-months
from this date.
This lesson teaches you 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.