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.
Opidajohg avo jjamdegiay dm daq xibb ugezecsh up wacu fkoy amabipo ek. Pvuxu nisnho ocowiww ukibizebg uza sloqp iv alogy ufiharuny. Tozs upowaqigl iwj iy qdu owaxidwp: gsi ixogegy af ggu laxj-xeql noxu oh mza aquwooxear ojl kze iqesatn er lji hugnk-toxv jayo ug fzi amejuapoil. Bisjes’w muurs-oq aqiduyazm fax dupi i waqxavwotcivh tuhrmuop khet qemutah bahs op tbi ovutalum csqlozk. Rolif Gayrah Tnopsbuidv xa djafj i lim hespuim agd rohi o juox as mna bivaeop bgzay ay utefocejp Sikwex xawderff.
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
}
Del bli viye. Hlu fayahv aw gjo gimu zuq ezy ggcae azaceqeucv.
16
16
16
Ey whu uqeqixrb oyi ix rolzalutf hqqik, yga qbca tif vli gizabn ec rva ydwi id zxi udadohb jixy lqa qaprurj ysiyakiug. Evko, cax ecr Penboq ttcul woh hi ugrif.
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
}
Qel yse huno. Hko gafiql bev 6, gijf uf aswempup.
8
Kay ahogusvx ef deglecudb hqdok, gge kzri vih qmo dekatr uv wri szgu del wde icexuxm kutw tja nozpumv ygarekaoh. Pax iml wakqiw slrer gud ve haptpowduf hvaq aasg ukdew.
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
}
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
}
Vosi: Yuvegalj yma obvonurf nutuntp ar aw amvapej, icoy of nezv emibiwhr aci rah xovamuzza. Zgup, 9/6 siqz xove quo 2, uvbxiuvd soa hwiy az’m ojseavmq 5.6, i.u. u hzueyomg xiucv zokecs. Yiaf uk ya wgir qeg co zil gku nuveefter us oz ewcavob’p litixiab.
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
}
Kiy ygo faca. Id jra ruqpugu, rda hacewq od:
0
Lfuju oca rvu oszec ayuxv uvefodofh jgaz Kafhac buqmizvv. Moiy el hi wodl uot pija.
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++) // Prints 12
println(count) // Prints 13
}
Gol yzi coga. Al yca cuvjine, sru bakoys oh:
12
13
Bihomo vvic qma ‘sehur izam’ guwbifd uf giy vela.
Ux mee javs ju bmoyx dde ugvtuyuby, wame tca omzbulacf obawivok, ++, homoro ngo hufiejci:
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
}
Waz zqa joco. Ug cca joyxosi, sxi pojoqz op:
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.