Operators
Operators are essential building blocks of any programming language, and Kotlin is no exception. Kotlin supports a wide range of operators that perform a variety of tasks, such as performing arithmetic, comparing values, and manipulating bits.
The sections that follow will review the main categories of Kotlin operators and demonstrate their basic implementation in code.
Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations, such as addition, subtraction, multiplication, and division. Kotlin supports the following arithmetic operators:
+
Addition: Returns the sum of the two arguments.-
Subtraction: Returns the difference of the two arguments.\*
Multiplication: Returns the product of the two arguments./
Division: Returns the quotient of the two arguments.%
Modulo: Returns the remainder based on the division of the two arguments.
Here’s an example of how to use arithmetic operators in Kotlin:
val a = 10val b = 5val c = a + b // c is now 15val d = a - b // d is now 5val e = a * b // e is now 50val f = a / b // f is now 2val g = a % b // g is now 0
Comparison Operators
Comparison operators are used to compare two values and determine whether they are equal, not equal, greater than, less than, greater than or equal to, or less than or equal to. Kotlin supports the following comparison operators:
==
Equals to: Returnstrue
if the arguments are equivalent.!=
Not equals to: Returnstrue
if the arguments are not equivalent.>
Greater than: Returnstrue
when the argument on the left is greater than the value on the right.<
Less than: Returnsfalse
when the argument on the left is greater than the value on the right.>=
Greater than or equals to: Returnstrue
when the argument on the left is greater than or equal to the value on the right.<=
Less than or equals to: Returnstrue
when the argument on the left is less than or equal to the value on the right.
Here’s an example of how to use comparison operators in Kotlin:
val a = 10val b = 5val c = 10val d = 15println(a == b) // falseprintln(a != b) // trueprintln(a > b) // trueprintln(a < b) // falseprintln(a >= c) // trueprintln(d <= b) // false
Logical Operators
Logical operators are used to combine two or more conditions and determine the overall result. Kotlin supports the following logical operators:
&&
And: Returnstrue
when the argument or expression on either side of the operator are bothtrue
.||
Or: Returnstrue
when one of the arguments or expressions in the statement aretrue
.!
Not: Returnstrue
when the argument or expression isfalse
, otherwisetrue
.
Here’s an example of how to use logical operators in Kotlin:
val a = 10val b = 5val c = 15println(a > b && a < c) // trueprintln(a > b || a > c) // trueprintln(!(a > b)) // false
Bitwise Operators
Bitwise operators are used to perform bitwise operations on binary values. Kotlin supports the following bitwise operators:
and
Bitwise and: Returns 1 when the both of the compared bits are 1, otherwise 0.or
Bitwise or: Returns 1 when one of the bits compared is 1, otherwise 0.xor
Bitwise xor: Returns 1 when one of the bits compared is 1 and the other is 0, if both bits are equivalent (0 or 1) 0 is returned.
Here’s an example of how to use bitwise operators in Kotlin:
val a = 0b1010val b = 0b1100println(a and b) // 8println(a or b) // 14println(a xor b) // 6println(b.inv()) // -13
Assignment Operators
Assignment operators are used to assign a value to a variable. Kotlin supports the following assignment operators:
=
Simple assignment: Used to specify default values for a parameters or the variable on the left.+=
Addition assignment: Adds a value and assigns the new total to the variable.-=
Subtraction assignment: Subtracts a value and assigns the new total to the variable.*=
Multiplication assignment: Takes the product of the arguments and assigns the new total to the variable./=
Division assignment: Takes the quotient of the arguments and assigns the new total to the variable.%=
Modulo assignment: Takes the remainder of a division of the arguments and assigns the new total to the variable.
Here’s an example of how to use assignment operators in Kotlin:
var a = 10var b = 5a += b // 15a -= b // 10a *= b // 50a /= b // 5a %= b // 0
Increment and Decrement Operators
++
Increment: Add one to the given value.--
Decrement: Subtract one from the given value.
Here’s an example of how to use the increment and decrement operators in Kotlin:
var a = 10a ++ // 11a -- // 10
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Kotlin on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Kotlin
Learn Kotlin, the expressive, open-source programming language developed by JetBrains.Beginner Friendly9 hours