Kotlin Operators

Kotlin Tutorial

Kotlin Operators – Complete Beginner Guide With Examples

Operators are one of the most fundamental concepts in any programming language. In Kotlin, operators are special symbols used to perform operations on variables and values.

If you’re starting your Kotlin journey, understanding operators will help you write logic, perform calculations, compare values, and control program flow effectively.

In this SEO-optimized, beginner-friendly guide, you’ll learn:

  • What operators are in Kotlin

  • Types of operators

  • Arithmetic operators

  • Assignment operators

  • Comparison operators

  • Logical operators

  • Unary operators

  • Bitwise operators

  • Elvis and safe-call operators

  • Real-world examples

  • Common beginner mistakes

Let’s begin


What Are Operators in Kotlin?

Operators are symbols that tell the compiler to perform specific mathematical, logical, or comparison operations.

Example:

Here:

  • + is an operator

  • 10 and 5 are operands

Operators work with variables and values to produce results.


Types of Operators in Kotlin

Kotlin provides several types of operators:

  1. Arithmetic Operators

  2. Assignment Operators

  3. Comparison Operators

  4. Logical Operators

  5. Unary Operators

  6. Bitwise Operators

  7. Null Safety Operators

Let’s explore each category in detail.


Arithmetic Operators in Kotlin

Arithmetic operators perform mathematical calculations.

OperatorMeaning
+Addition
Subtraction
*Multiplication
/Division
%Modulus

Example: Arithmetic Operators


 

Output:

13
7
30
3
1

Assignment Operators in Kotlin

Assignment operators assign values to variables.

OperatorExampleMeaning
=x = 5Assign value
+=x += 3Add and assign
-=x -= 2Subtract and assign
*=x *= 2Multiply and assign
/=x /= 2Divide and assign
%=x %= 2Modulus and assign

Example

Output:

15

Comparison Operators in Kotlin

Comparison operators compare two values and return Boolean results.

OperatorMeaning
==Equal to
!=Not equal
>Greater than
<Less than
>=Greater or equal
<=Less or equal

Example


 

Output:

false
true
false

Logical Operators in Kotlin

Logical operators combine multiple conditions.

OperatorMeaning
&&Logical AND
!Logical NOT

Example


 

Output:

true

Logical operators are commonly used in decision-making.


Unary Operators in Kotlin

Unary operators operate on a single operand.

OperatorMeaning
+Positive
Negative
++Increment
Decrement
!Logical NOT

Example

Output:

6

Pre-Increment vs Post-Increment

Difference:

  • ++x → increments first

  • x++ → increments after use


Bitwise Operators in Kotlin

Kotlin does not use traditional bitwise symbols like & and |.

Instead, it provides functions:

FunctionMeaning
andBitwise AND
orBitwise OR
xorBitwise XOR
shlShift left
shrShift right

Example


 


Null Safety Operators in Kotlin

Kotlin provides special operators for null safety.


Safe Call Operator (?.)

This prevents NullPointerException.


Elvis Operator (?:)

If left side is null, right side is returned.


Not-Null Assertion (!!)

Use carefully — may throw exception.


Real-World Example – Login System


 

Operators make this possible.


Operator Precedence in Kotlin

Order of execution:

  1. Unary operators

  2. Multiplication and division

  3. Addition and subtraction

  4. Comparison

  5. Logical AND

  6. Logical OR

Example:

Output:

20

Multiplication runs first.


Common Beginner Mistakes

 Using = Instead of ==

= assigns value
== compares value


 Confusing && and ||

&& requires both conditions true
|| requires only one


Ignoring Null Safety

Always use ?. or ?: when working with nullable variables.


Operators vs Functions

Some operators are actually translated into function calls.

Example:

a + b

Internally becomes:

a.plus(b)

Kotlin supports operator overloading.


When Should You Use Operators?

Use operators when:

  • Performing calculations

  • Comparing values

  • Combining conditions

  • Managing null values

  • Writing logical expressions

Keep expressions readable and simple.


Frequently Asked Questions (FAQs)

1. What are operators in Kotlin?

Operators are symbols used to perform operations on variables and values.

2. What is the difference between == and ===?

== checks structural equality.
=== checks reference equality.

3. What is the Elvis operator?

The Elvis operator (?:) provides a default value if a variable is null.

4. What is the safe call operator?

The safe call operator (?.) safely accesses properties of nullable objects.

5. Does Kotlin support bitwise operators?

Yes, but they are implemented as functions like and, or, xor, shl, shr.


Conclusion

Kotlin operators are essential for writing logical, mathematical, and conditional expressions.

You learned:

  • Arithmetic operators

  • Assignment operators

  • Comparison operators

  • Logical operators

  • Unary operators

  • Bitwise operations

  • Null safety operators

Mastering operators will make your Kotlin programming more efficient and powerful.

You may also like...