Introduction
Operators let you perform computations and comparisons on values. Kotlin supports the familiar arithmetic, comparison, and logical operators found in most languages, plus a few Kotlin-specific ones: the range operator (..) for creating ranges, and the in / !in operators for checking membership. One important gotcha for developers coming from Java is how equality works: Kotlin's == checks structural equality by calling equals(), while === checks referential equality (whether two references point to the same object).
Cricket analogy: Comparing two batsmen's stats with < and > is like arithmetic operators, but asking 'is this the same Virat Kohli' (===) differs from 'does this player have identical stats' (==), which checks content not identity.
Syntax
val sum = a + b // arithmetic
val isEqual = a == b // structural equality
val isSameRef = a === b // referential equality
val bothTrue = x && y // logical AND
val within = 1..10 // range
val contained = 5 in 1..10 // containment checkExplanation
Arithmetic operators (+, -, *, /, %) work as expected on numeric types. Comparison operators (<, >, <=, >=) compare ordered values. Logical operators (&&, ||, !) combine boolean expressions. The key difference from Java is equality: in Java, == on objects compares references, but in Kotlin == is overloaded to call the equals() method, so it compares content (structural equality). To check whether two variables reference the exact same object in memory, use ===. The range operator .. creates a range such as 1..10, which is commonly used with the in and !in operators to test whether a value falls inside or outside that range.
Cricket analogy: Adding a bowler's wickets across matches uses +, and checking if a batting average is >= 50 uses comparison operators, while '&&' combines 'scored a century && took 5 wickets' as an all-round performance check.
Example
fun main() {
val a = 200
val b = 200
println(a == b) // structural equality
println(a === b) // referential equality (may differ for boxed values)
val score = 75
println(score in 1..100) // containment
println(score !in 1..50) // negated containment
val passed = score >= 40 && score <= 100
println(passed)
}Output
true
true
true
true
trueKey Takeaways
- Kotlin supports arithmetic (+, -, *, /, %), comparison (<, >, <=, >=), and logical (&&, ||, !) operators.
- == checks structural equality by invoking equals(), unlike Java where == on objects checks references.
- === checks referential equality, i.e. whether two variables point to the exact same object.
- The range operator .. creates a range, e.g. 1..10, often used with for loops or containment checks.
- in and !in test whether a value is contained (or not) within a range or collection.
Practice what you learned
1. In Kotlin, what does the == operator check?
2. What does the === operator check in Kotlin?
3. What does the range expression 1..10 represent?
4. Which operator checks if a value is NOT contained in a range or collection?
5. How does Kotlin's == differ from Java's == when comparing two objects?
Was this page helpful?
You May Also Like
Variables and Data Types in Kotlin
Learn how Kotlin declares variables with val and var, and the built-in data types used to store values.
Ranges in Kotlin
Learn how Kotlin ranges express sequences of values for iteration and containment checks using .., until, downTo, and step.
if-else Expressions in Kotlin
Learn how Kotlin's if-else works as both a statement and a value-returning expression, replacing the ternary operator.
Type Conversion in Kotlin
Understand why Kotlin requires explicit conversion between numeric types and how to use functions like toInt() and toDouble().
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics