Kotlin is a statically typed programming language that runs on the Java Virtual Machine (JVM). It was developed by JetBrains and is now an official language for Android development. Kotlin is designed to be concise, expressive, and safe. It is also interoperable with Java, which means that you can use Kotlin and Java code together in the same project.
Overview about the Lesson
This lesson is part of a series of lessons on Kotlin programming. In this lesson, we will cover the operators in Kotlin. We will start by discussing what operators are and how they work.
We will then explore the different types of operators in Kotlin, including arithmetic operators, comparison operators, and logical operators. Finally, we will look at some examples of how to use operators in Kotlin.



The Content Of The Lesson
The content of this lesson is divided into the following sections:
- What are operators?
- Arithmetic operators
- Comparison operators
- Logical operators
- Examples
Why This Lesson
This lesson is important because operators are an essential part of any programming language. They allow us to perform various operations on data types, which is essential for building complex applications. By understanding the operators in Kotlin, you will be able to write more efficient and effective code.
Kotlin Operators: Examples and Output
This lesson’s PDF introduces arithmetic, comparison and logical operators. Run these examples to see exactly what each operator returns.
Arithmetic operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
| + | Addition | 7 + 3 | 10 |
| – | Subtraction | 7 - 3 | 4 |
| * | Multiplication | 7 * 3 | 21 |
| / | Division | 7 / 3 | 2 (Int division) |
| % | Remainder (modulo) | 7 % 3 | 1 |
| / with Double | Decimal division | 7.0 / 3 | 2.3333333333333335 |
The remainder operator is handy for checks such as “is this number even?”: n % 2 == 0.
Assignment and increment operators
fun main() {
var x = 10
x += 5 // x = 15
x -= 3 // x = 12
x *= 2 // x = 24
x /= 4 // x = 6
x %= 4 // x = 2
x++ // x = 3
println(x) // 3
}Comparison operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
| == | Equal (compares values) | "kotlin" == "kotlin" | true |
| != | Not equal | 5 != 3 | true |
| > / < | Greater / less than | 5 > 3 | true |
| >= / <= | Greater / less than or equal | 5 <= 5 | true |
| === | Same object (reference equality) | a === b | depends on the objects |
In Kotlin, == compares content (it calls equals()), so you can compare strings safely with ==, unlike Java.
Logical operators
fun main() {
val age = 22
val hasTicket = true
println(age >= 18 && hasTicket) // true: both conditions
println(age < 12 || age > 65) // false: neither condition
println(!hasTicket) // false: negation
}&& and || are short-circuiting: if the left side already decides the answer, the right side is not evaluated.
Operator precedence
Kotlin evaluates * / % before + -, then comparisons, then &&, then ||. So 2 + 3 * 4 is 14. Use parentheses whenever the order is not obvious.
Practice exercise
Check whether a year is a leap year: divisible by 4 and not by 100, or divisible by 400.
fun main() {
val year = 2028
val isLeap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0
println("$year leap year? $isLeap") // 2028 leap year? true
}Frequently Asked Questions
Why does 7 / 2 return 3 in Kotlin?
Both values are Int, so Kotlin performs integer division and drops the remainder. Write 7.0 / 2 or 7 / 2.0 to get 3.5.
What is the difference between == and ===?
== checks whether two values are equal; === checks whether two references point to the very same object in memory.
Continue the course
← Previous: Lesson 4: Data Types | Next: Lesson 6: Set Operations & Grouping →
- Lesson 1: Getting Started
- Lesson 2: Syntax & Output
- Lesson 3: Comments & Variables
- Lesson 4: Data Types
- Lesson 5: Operators (you are here)
- Lesson 6: Set Operations & Grouping
- Lesson 7: Booleans & If…Else
- Lesson 8: When, While, Break & Continue
- Lesson 9: Arrays
- Lesson 10: For Loops & Ranges
- Lesson 11: Functions & Classes
- Lesson 12: Classes, Objects & Constructors
- Lesson 13: Class Functions & Inheritance
Conclusion
In conclusion, Kotlin provides a rich set of operators that can be used to perform various operations on data types. In this lesson, we covered the different types of operators in Kotlin, including arithmetic operators, comparison operators, and logical operators. We also looked at some examples of how to use operators in Kotlin. By understanding the operators in Kotlin, you will be able to write more efficient and effective code.
Download From Link
You can download the lesson from the following link:














