Kotlin has a set of operators to perform arithmetic, assignment, comparison operators and more. You will learn to use these operators in this article.
So far we have looked at using variables and constants in Kotlin and also described the different data types. Being able to create variables is only part of the story however. The next step is to learn how to use these variables in Kotlin code. The primary method for working with data is in the form of expressions.
An operator is a special symbol which indicates a certain process is carried out. Operators in programming languages are taken from mathematics. Programmers work with data. The operators are used to process data. An operand is one of the inputs (arguments) of an operator.
Expressions are constructed from operands and operators. The operators of an expression indicate which operations to apply to the operands. The order of evaluation of operators in an expression is determined by the precedence and associativity of the operators.
Operators are special symbols (characters) that carry out operations on operands (variables and values). For example, + is an operator that performs addition.
In Java variables article, you learned to declare variables and assign values to variables. Now, you will learn to use operators perform various operations on them.
How arithmetic operators actually work?
Suppose, you are using + arithmetic operator to add two numbers a and b.
Under the hood, the expression a + b calls a.plus(b) member function. The plus operator is overloaded to work with String values and other basic data types (except Char and Boolean).
We have already looked at the most basic of assignment operators, the = operator. This assignment operator simply assigns the result of an expression to a variable. In essence, the = assignment operator takes two operands. The left-hand operand is the variable to which a value is to be assigned and the right-hand operand is the value to be assigned. The right-hand operand is, more often than not, an expression which performs some type of arithmetic or logical evaluation or a call to a function, the result of which will be assigned to the variable. The following examples are all valid uses of the assignment operator.