Kotlin Syntax & Language Basics
Estimated reading: 3 minutes 30 views

➕ Kotlin – Operators: Arithmetic, Logical, Relational & More

🧲 Introduction – Why Learn Kotlin Operators?

Operators are symbols that perform computations, comparisons, and logic operations on variables and values. Kotlin supports a wide range of operators similar to Java but with added clarity and null-safety. Mastering Kotlin operators allows you to write efficient, readable, and expressive code.

🎯 In this guide, you’ll learn:

  • All major categories of Kotlin operators
  • Syntax and examples for arithmetic, logical, and relational operations
  • How Kotlin handles null-safe operations and operator overloading
  • Best practices for operator usage

🧮 Arithmetic Operators

Used to perform basic mathematical operations.

OperatorNameExampleOutput
+Addition10 + 515
-Subtraction10 - 55
*Multiplication10 * 550
/Division10 / 25
%Modulus10 % 31

✨ Example:

val a = 12
val b = 4
println("Sum: ${a + b}, Product: ${a * b}")

✅ Output:

Sum: 16, Product: 48

⚖️ Relational (Comparison) Operators

Used to compare values. They return true or false.

OperatorDescriptionExampleOutput
==Equal to5 == 5true
!=Not equal to5 != 3true
>Greater than6 > 2true
<Less than2 < 5true
>=Greater than or equal3 >= 3true
<=Less than or equal2 <= 1false

🔁 Assignment Operators

Used to assign and update values in variables.

OperatorDescriptionExampleEquivalent To
=Assignmenta = 10
+=Add and assigna += 5a = a + 5
-=Subtract and assigna -= 3a = a - 3
*=Multiply and assigna *= 2a = a * 2
/=Divide and assigna /= 2a = a / 2
%=Modulo and assigna %= 3a = a % 3

⚙️ Unary Operators

Operate on a single operand.

OperatorMeaningExample
+Unary plus+a
-Unary minus-a
++Incrementa++
--Decrementa--
!Logical NOT!true

🤔 Logical Operators

Used to build compound boolean expressions.

OperatorDescriptionExampleResult
&&Logical ANDtrue && falsefalse
``Logical OR
!Logical NOT!truefalse
val age = 25
val isMember = true

if (age > 18 && isMember) {
    println("Eligible")
}

✅ Output:

Eligible

🔎 Kotlin Identity & Equality Operators

OperatorDescriptionExample
==Structural equalitya == b (content match)
===Referential equalitya === b (same object)
val str1 = "Hello"
val str2 = "Hello"
println(str1 == str2)   // true
println(str1 === str2)  // might be true (same reference in memory)

❔ Null-Safety Operators

Kotlin introduces special operators for handling nulls safely.

OperatorNameExamplePurpose
?.Safe callname?.lengthAvoids NullPointerException
?:Elvis operatorname ?: "Guest"Default value if null
!!Not-null assertname!!Throws NPE if null

💡 Operator Overloading in Kotlin

Kotlin allows custom operator overloading for classes using predefined keywords.

✅ Example:

data class Point(val x: Int, val y: Int) {
    operator fun plus(other: Point) = Point(x + other.x, y + other.y)
}

val p1 = Point(2, 3)
val p2 = Point(4, 1)
println(p1 + p2) // Output: Point(x=6, y=4)

📌 Summary – Recap & Next Steps

Kotlin offers a rich set of operators that make arithmetic, logic, and comparison tasks intuitive and expressive. It also introduces null-safe operators and operator overloading, making Kotlin highly flexible and safe.

🔍 Key Takeaways:

  • Use arithmetic, logical, and relational operators like in Java.
  • Kotlin supports structural (==) and referential (===) equality.
  • Use null-safety operators to avoid NullPointerExceptions.
  • Operator overloading enables expressive custom logic in classes.

⚙️ Practical Use:
Operators are critical in every Kotlin application—from simple calculations to complex conditions in Android, backend services, and business logic.


❓ FAQs – Kotlin Operators

What’s the difference between == and === in Kotlin?
== checks value equality, while === checks reference equality (if two variables point to the same object).


How does Kotlin handle nulls with operators?
✅ Use the safe call operator ?., the Elvis operator ?:, or !! for asserting non-null.


Can I overload operators in Kotlin?
✅ Yes. Kotlin allows operator overloading by defining functions with the operator keyword in classes.


Does Kotlin support all Java-style operators?
✅ Yes, including arithmetic, comparison, assignment, and logical operators—plus Kotlin adds null-safety operators.


Can I use ++ and -- in Kotlin like in Java?
✅ Yes. Kotlin supports a++, --b, etc., for increment/decrement operations.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Kotlin – Operators (Arithmetic, Logical, Relational, etc.)

Or Copy Link

CONTENTS
Scroll to Top