In this tutorial, our goal is to understand and explore the different types of operators in Kotlin and their usage. By the end of this tutorial, you will understand how to use various operators in Kotlin to create expressions and build complex programming logic.
Prerequisites:
- Basics of Kotlin Programming
- Basic understanding of programming concepts like variables, data types, etc.
Operators are special symbols or keywords that are used to perform specific operations. Kotlin supports a rich set of operators, we will focus on Arithmetic, Relational, Assignment, Unary and Logical operators.
Arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, and modulus.
val a = 10
val b = 20
println(a + b) // prints 30 - Addition
println(a - b) // prints -10 - Subtraction
println(a * b) // prints 200 - Multiplication
println(a / b) // prints 0 - Division
println(a % b) // prints 10 - Modulus
Relational operators are used to compare two values. They return a boolean value - either true or false.
val a = 10
val b = 20
println(a == b) // prints false - Equality check
println(a != b) // prints true - Non-equality check
println(a > b) // prints false - Greater than check
println(a < b) // prints true - Less than check
println(a >= b) // prints false - Greater than or equal to check
println(a <= b) // prints true - Less than or equal to check
Assignment operators are used to assign a value to a variable.
var a = 10
a += 20 // Equivalent to a = a + 20
println(a) // prints 30
a -= 10 // Equivalent to a = a - 10
println(a) // prints 20
a *= 2 // Equivalent to a = a * 2
println(a) // prints 40
a /= 4 // Equivalent to a = a / 4
println(a) // prints 10
a %= 3 // Equivalent to a = a % 3
println(a) // prints 1
Unary operators are used to increment or decrement a value.
var a = 10
a++ // Increment operator
println(a) // prints 11
a-- // Decrement operator
println(a) // prints 10
Logical operators are used to perform logical operations such as AND, OR, and NOT.
val a = true
val b = false
println(a && b) // prints false - Logical AND
println(a || b) // prints true - Logical OR
println(!a) // prints false - Logical NOT
In this tutorial, we learned about various Kotlin operators - Arithmetic, Relational, Assignment, Unary, and Logical. We saw how to use them to create expressions and build complex programming logic.
For further learning, you can explore other Kotlin concepts like control flow statements, functions, classes, etc.
// Solution for Exercise 1
val num1 = 10
val num2 = 20
println(num1 + num2) // prints 30
println(num1 - num2) // prints -10
println(num1 * num2) // prints 200
println(num1 / num2) // prints 0
// Solution for Exercise 2
println(num1 == num2) // prints false
println(num1 != num2) // prints true
println(num1 > num2) // prints false
println(num1 < num2) // prints true
// Solution for Exercise 3
val condition1 = true
val condition2 = false
println(condition1 && condition2) // prints false
println(condition1 || condition2) // prints true
println(!condition1) // prints false
After practicing these exercises, try to create more complex expressions using the operators you've learned.