Kotlin Operators Tutorial: Arithmetic, Relational, Logical, Assignment, Unary, in and is Operators
This Kotlin Operators tutorial explains all major operators used in Kotlin programming, including arithmetic, relational, logical, assignment, unary, and special operators such as in and is. Each operator type is explained with clear syntax, practical examples, and best practices to help beginners and professionals write correct, readable, and efficient Kotlin code.
Kotlin Operators – Complete Tutorial
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations.
Operators
+Addition-Subtraction*Multiplication/Division%Modulus
Example
Best Practices
- Use meaningful variable names for clarity.
- Be careful with integer division; use
Doubleif precision is required.
Relational Operators
Relational operators compare two values and return a Boolean result.
Operators
>Greater than<Less than>=Greater than or equal to<=Less than or equal to==Equal to!=Not equal to
Example
Best Practices
- Use
==for value comparison, not reference comparison. - Avoid complex comparisons in a single statement.
Logical Operators
Logical operators are used to combine multiple Boolean expressions.
Operators
&&Logical AND||Logical OR!Logical NOT
Example
Best Practices
- Keep Boolean expressions simple and readable.
- Use parentheses for clarity when needed.
Assignment Operators
Assignment operators assign values to variables.
Operators
=Assign+=Add and assign-=Subtract and assign*=Multiply and assign/=Divide and assign%=Modulus and assign
Example
Best Practices
- Use compound assignment operators to reduce verbosity.
- Avoid modifying the same variable excessively in one block.
Unary Operators
Unary operators operate on a single operand.
Operators
+Unary plus-Unary minus++Increment--Decrement!Logical NOT
Example
Best Practices
- Prefer readable expressions over excessive increment or decrement usage.
- Avoid using
++and--inside complex expressions.
in Operator
The in operator checks whether a value exists within a range or collection.
Example with Range
Example with Collection
Best Practices
- Use
infor clean and readable range checks. - Prefer
inover manual loops.
is Operator
The is operator checks the type of an object at runtime.
Example
Best Practices
- Use
isfor safe type checking. - Take advantage of smart casting after
ischecks.
Summary
This chapter covered all essential Kotlin operators, including arithmetic, relational, logical, assignment, unary, and special operators like in and is. Understanding these operators is fundamental for writing logical conditions, loops, and expressions in Kotlin programs.