Operators in Kotlin
Learn arithmetic, comparison, logical, and assignment operators in Kotlin, plus operator overloading and range operators
Operators are the building blocks of expressions in any programming language. Kotlin provides a rich set of operators that go beyond the standard arithmetic and logical operators you’d expect from a JVM language. As a multi-paradigm language with both object-oriented and functional roots, Kotlin treats many operators as function calls behind the scenes — a + b is actually shorthand for a.plus(b).
This design has powerful consequences. Operators can be overloaded for your own types, ranges become first-class citizens with the .. operator, and null-safety operators like ?. and ?: help eliminate entire categories of runtime errors. Kotlin also drops some legacy operators (like the bitwise & and | you’d find in Java) in favor of named infix functions such as and and or.
In this tutorial you’ll learn the full operator landscape: arithmetic, comparison, logical, assignment, range, and the null-safety operators that make Kotlin distinctive.
Arithmetic and Assignment Operators
Kotlin supports the standard arithmetic operators (+, -, *, /, %) and their compound assignment forms (+=, -=, *=, /=, %=). Note that integer division truncates toward zero, just like Java.
Create a file named Operators.kt:
| |
Operator Precedence
Kotlin’s operator precedence follows mathematical conventions: *, /, % bind tighter than +, -, which bind tighter than comparisons, which bind tighter than && and ||. Use parentheses when in doubt — the compiler won’t complain, and the intent will be clearer to readers.
| |
Ranges, Equality, and Null-Safety Operators
Kotlin introduces several operators that don’t exist in Java. The range operator .. creates a Range object, === checks referential identity (distinct from structural equality ==), and the safe-call ?. and Elvis ?: operators make working with nullable values painless.
Create a file named KotlinOperators.kt:
| |
Running with Docker
| |
Expected Output
Running Operators.kt:
a + b = 22
a - b = 12
a * b = 85
a / b = 3
a % b = 2
x / y = 3.4
counter = 26
n = 3
a == b is false
a != b is true
a > b is true
a <= b is false
isAdult && hasTicket = false
isAdult || hasTicket = true
!isAdult = false
Hello, Kotlin!
and = 8, or = 15, shl = 8
Running KotlinOperators.kt:
3 in 1..5 = true
7 in 1..5 = false
5 in 1 until 5 = false
Countdown: 5 3 1
s1 == s2 (structural) = true
s1 === s3 (referential) = true
length via safe call = null
length with default = 0
forced length = 7
'apple' in fruits = true
'grape' !in fruits = true
uppercase = KOTLIN
Key Concepts
- Operators are functions —
a + bdesugars toa.plus(b), so any class can overload arithmetic operators by implementing the right operator functions. - Structural vs referential equality — Use
==for value equality (callsequals()) and===for identity (same object reference). This is the opposite of Java’s defaults. - No bitwise operator symbols — Kotlin uses infix functions (
and,or,xor,shl,shr,inv) instead of&,|, etc. This keeps&&and||unambiguous. - Ranges are objects —
1..10,1 until 10, and10 downTo 1produce iterable ranges you can use inforloops,inchecks, and collection operations. - Null-safety operators eliminate NPEs —
?.short-circuits on null,?:provides defaults, and!!asserts non-null (throws if wrong). Prefer the first two; reserve!!for cases where null is genuinely a bug. - Smart casts — After an
ischeck, the compiler automatically casts the variable inside the branch, so you don’t need to write the cast explicitly. inworks on ranges and collections — The same operator checks membership in a numeric range or a list, making expressions read naturally.
Comments
Loading comments...
Leave a Comment