Skip to main content
Kotlin Basics
CHAPTER 05 Beginner

Operators and Expressions

Updated: May 18, 2026
5 min read

# CHAPTER 5

Operators and Expressions

1. Chapter Introduction

An operator is a special symbol that tells the compiler to perform a specific mathematical or logical manipulation. Kotlin includes standard operators found in almost all programming languages (like +, -, ==), but it also introduces several modern, Kotlin-specific operators (like the Safe Call ?. and Elvis ?: operators) designed to write incredibly safe and concise code.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Perform basic math using Arithmetic Operators.
  • Compare values using Relational/Comparison Operators.
  • Combine conditions using Logical Operators.
  • Understand Assignment Operators.
  • Recognize Kotlin's unique Null Safety operators.

3. Arithmetic Operators

These are used for basic mathematics.
  • Addition: +
  • Subtraction: -
  • Multiplication: *
  • Division: /
  • Modulus (Remainder): %
kotlin
12345678
fun main() {
    val a = 10
    val b = 3
    
    println("Addition: " + (a + b))       // 13
    println("Division: " + (a / b))       // 3 (Because both are Ints, the decimal is dropped!)
    println("Modulus: " + (a % b))        // 1 (The remainder of 10 divided by 3)
}

4. Assignment Operators

Used to assign values to variables.
  • Standard: =
  • Add and Assign: +=
  • Subtract and Assign: -=
  • Multiply and Assign: *=
  • Divide and Assign: /=
kotlin
12345
fun main() {
    var score = 10
    score += 5  // Equivalent to: score = score + 5
    println(score) // 15
}

*Note: You cannot use Assignment operators on val variables, only var!*

5. Comparison (Relational) Operators

Used to compare two values. They always evaluate to a Boolean (true or false).
  • Equal to: ==
  • Not Equal to: !=
  • Greater than: >
  • Less than: <
  • Greater than or equal: >=
  • Less than or equal: <=
kotlin
12345678
fun main() {
    val a = 5
    val b = 10
    
    println(a == b) // false
    println(a != b) // true
    println(a < b)  // true
}

*In Java, == compares memory references for Objects. In Kotlin, == automatically checks the actual data (structural equality).*

6. Logical Operators

Used to combine multiple boolean expressions.
  • AND: && (Returns true ONLY if BOTH sides are true)
  • OR: || (Returns true if AT LEAST ONE side is true)
  • NOT: ! (Reverses the boolean value)
kotlin
12345678
fun main() {
    val hasTicket = true
    val isVip = false
    
    println(hasTicket && isVip) // false (Both must be true)
    println(hasTicket || isVip) // true (Only one needs to be true)
    println(!hasTicket)         // false (Reverses true to false)
}

7. Kotlin's Special Operators (Preview)

Kotlin was designed to eliminate Null Pointer Exceptions. It introduces unique operators specifically for handling variables that might be missing data (null). We will cover these deeply in Chapter 12, but you should recognize them now.

#### The Safe Call Operator ?. If you try to access a property on an object that is null, Java crashes. Kotlin allows you to use ?.. It says: "If the variable is not null, execute this. If it is null, just return null safely and don't crash!"

kotlin
123
// Assume 'name' might be null
var name: String? = null
println(name?.length) // Prints "null" instead of crashing!

#### The Elvis Operator ?: Named because it looks like Elvis Presley's hair emoji! It provides a "default" value if the variable on the left is null.

kotlin
1234
var name: String? = null
// If 'name' is null, use "Guest" instead
val displayName = name ?: "Guest" 
println(displayName) // Prints "Guest"

8. Increment and Decrement

You can easily add or subtract 1 from a variable using ++ and --.
kotlin
123
var i = 0
i++ // i is now 1
i-- // i is back to 0

9. Common Mistakes

  • Integer Division: Dividing 10 / 3 in Kotlin yields 3, not 3.333. Because both numbers are Integers, the result is forced to be an Integer. To get a decimal, at least one number must be a Float or Double: 10.0 / 3.
  • Using = instead of ==: Writing if (a = b) is a syntax error in Kotlin. = is for assignment. == is for comparison.

10. Best Practices

  • Parentheses for Clarity: Even when order of operations (PEMDAS) dictates the math, use parentheses to make complex logical expressions readable for humans: if ((x > 5) && (y < 10))

11. Exercises

  1. 1. Declare two var variables, x = 15 and y = 4.
  1. 2. Print the remainder of x divided by y.
  1. 3. Use += to add 5 to x.
  1. 4. Print the boolean result of whether x is now greater than 15.

12. MCQs with Answers

Question 1

Which operator calculates the remainder of division?

Question 2

What is the output of println(5 / 2) in Kotlin?

Question 3

Which operator is used to check if two values are equal?

Question 4

What does x += 3 do?

Question 5

In the expression true && false, what is the result?

Question 6

Which Logical Operator reverses a boolean value?

Question 7

What is the name of the ?: operator in Kotlin?

Question 8

What does the Elvis operator ?: do?

Q9. What does the Safe Call operator ?. do? a) safely executes a method/property if the object is NOT null, returning null instead of crashing if it IS null b) Always returns true Answer: a) Safely executes if not null, otherwise returns null.

Q10. Can you use x++ if x was declared using val? a) Yes b) No, because val is immutable and cannot be reassigned Answer: b) No.

13. Interview Questions

  • Q: Explain how the == operator differs in Kotlin compared to Java. (Answer: In Java, == checks for reference equality (if they point to the same memory). In Kotlin, == calls the .equals() method under the hood to check for structural/data equality).
  • Q: Explain the purpose of the Elvis operator ?: with an example.

14. Summary

Operators are the foundation of logic. While Kotlin implements the standard arithmetic and logical operators you expect from any language, its real power lies in the modern Safe Call (?.) and Elvis (?:) operators. These tools allow you to write concise, null-safe expressions without needing massive if/else checks.

15. Next Chapter Recommendation

Now that we can manipulate data, let's learn how to get that data from the user. In Chapter 6: User Input and Output, we will learn how to read terminal input and inject variables into Strings effortlessly using String Templates.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·