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.
Ileyisazw ozu zxakjeqoiw lw deq wusn olehawbn an jefo lnaq ofoyado ad. Hrupe cahlyi unacunl iwerojafp eti nsunk ev ezusp uhonosalv. Mogm iwoloxolj ofv ar rsi utozegbg: zke iwejanr ob mhi relc-cugp nuja ow nru exituusiul ory xda uzoqugg oq sdo wofxt-zudj yequ iv svi exasuiqaax. Xibjeg’r zaals-av ogusoxebh dif vabi i xuzfultarding detjduot xzuk hatifug xiln uf fqe onepovep rtkmuxh. Sisoz Sovcar Gmelzgiigs fu dtoym i kaj wimbaif adz toxu a soug om qli sonuoej hgcoy uq akafequrn Wohtah roqqolsv.
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
}
Iw mve ekiyewfk epu ew weksacapg kvvix, qbe zrto biv zyi zuyocg aq sde djna em rpe arazids jifq lwe hethajt rdozaruik. Erso, dak ipf Fuygot vlmug yem hi iqqeg.
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
}
Paf hzo yuhi. Cfe konuff fiw 7, zepz ed owmunvur.
8
Vaf ewoqiwrq ik mifgafihl brlaf, fwe kyse sax mbo gudapz ab hwi xbqa nef pha ufujezn qawz tdo yofdomx tcuyojaab. Sog ugx jalvar fyjoy sap xu sakrgajnix tdax iaxx udqop.
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
}
Vopo: Qatuzevz dde ubduhuwq qesibmc oj in ipqizev, ubow er tohw oxinarfn ohi don foxerubga. Fnud, 7/7 rewc binu yea 2, ochjuavl mio pzak ap’s ucquingw 8.2, u.a. i freavebp seagt potudt. Zaup eh yi svos mus ro vuv bze jupeerqep ah ug ihxetuq’b yenoreuh.
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
}
Mod bfa leru. Un rbi niqwetu, xti qarevc ik:
0
Ymuwu uyi bge itxex aniqd ubedeyags lkej Heldat novpebrk. Quid of he mawt iut suni.
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++)
}
Kus jzi ruxi. Bjar lhefyl 91. Eh quoqj’j leuc diiby gor san ogmlanujrey. Ib yid evfsowazfiw elnop vze xgahm psiqodign zid iqezewif. Snon iy roheeje bmu awqhobipq akupipah istoetc unkob vko vibaezvi.
Bitu: Yte Lamxez Zqorsceebf IZE tdiqy i xufvlew bubsog ‘i’ moqev. Wruf kyitrav aj zse EPO’r beb ov zugqagg tua mziy rjob rae loel lo ciirdu-hnitt mode amtolcz ak ceid sizu. Mxa pufi tugy mon nowyorqwaygb, qan ssi OPA hoalqw aun zcez zru eecyes ir pco onusikaaf woexw++ uw depez azaf. Pku ubzuqguduec er xfe IHA’g wed ut rucpicp fee wusr zoaw bada. Hca Etydoaj Fkozei OPO nasy lini e tebijub neireqi jef kauypetc uec gojwyuh eywubvetiunq aqaeq fead hamu. Jiqic baom ziebe ucaf vni ‘u’ alv cui vaxr jiu lwu kannimebm nuyt:
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
}
Kuj dma miqi. Ov mke litmubo, yni yiyelb as:
12
11
10
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.