Operators and Expressions
# 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):
%
4. Assignment Operators
Used to assign values to variables.-
Standard:
=
-
Add and Assign:
+=
-
Subtract and Assign:
-=
-
Multiply and Assign:
*=
-
Divide and Assign:
/=
*Note: You cannot use Assignment operators on val variables, only var!*
5. Comparison (Relational) Operators
Used to compare two values. They always evaluate to aBoolean (true or false).
-
Equal to:
==
-
Not Equal to:
!=
-
Greater than:
>
-
Less than:
<
-
Greater than or equal:
>=
-
Less than or equal:
<=
*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)
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!"
#### 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.
8. Increment and Decrement
You can easily add or subtract 1 from a variable using++ and --.
9. Common Mistakes
-
Integer Division: Dividing
10 / 3in Kotlin yields3, not3.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==: Writingif (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.
Declare two
varvariables,x = 15andy = 4.
-
2.
Print the remainder of
xdivided byy.
-
3.
Use
+=to add 5 tox.
-
4.
Print the boolean result of whether
xis now greater than 15.
12. MCQs with Answers
Which operator calculates the remainder of division?
What is the output of println(5 / 2) in Kotlin?
Which operator is used to check if two values are equal?
What does x += 3 do?
In the expression true && false, what is the result?
Which Logical Operator reverses a boolean value?
What is the name of the ?: operator in Kotlin?
What does the Elvis operator ?: do?
?. 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.