Kotlin Tutorial
Kotlin Classes
Operators
In Kotlin, we have various operators to perform different operations like arithmetic, assignment, logical, and comparison operators etc.
While performing any operations on values we have operators & operands. The value on which an operation is performed is operand, while the operation to be performed between two operands is called operator.
In the example below 10 and 5 are operands, while the + between them is operator.
Example:
var a = 10 + 5
println(a)
The code above outputs:
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations.
Example of Arithmetic operators:
var a = 10
var b = 50
var result = 0
result = a + b
println("a + b = $result")
result = a - b
println("a - b = $result")
result = a * b
println("a * b = $result")
result = a / b
println("a / b = $result")
result = a % b
println("a % b = $result")
result = ++a
println("a increment = $result")
result = --b
println("b decrement = $result")
The code above outputs:
a + b = 60
a – b = -40
a * b = 500
a / b = 0
a % b = 10
a increment = 11
b decrement = 49
Assignment Operators
Assignment operator is used to assign a value to a variable. We have been using this operator for a long time. That is the “ = “ operator.
In the example below we assign the value 10 to the variable a.
Example:
var a = 10
Example of Assignment operators.
// Assign 10 to variable a
var a = 10
println(a)
// Add 10 - using addition assignment operator
a += 10
println(a)
// Subtract 10 - using subtraction assignment operator
a -= 10
println(a)
// And so on
The code above outputs:
10
20
10
Comparison Operators
Comparison operators are used to compare two values and result in true or false which means a Boolean value.
Example of comparison operators.
var a = 10
var b = 20
// a is equal to b ( == )
println(a == b)
// a is greater than b ( > )
println(a > b)
// a is less than b ( < )
println(a < b)
// a is not equal to b ( != )
println(a != b)
// and so on
The code above outputs:
false
false
true
true
You will learn more about conditional statements in later chapters
Logical Operators
In Kotlin, There are 3 logical operators. And are used in conditional statements (if, else).
They are:
AND ( && ) Logical operator.
In logical operator AND or && (1 and 1 = true) means both conditions must be true in case the result will be true otherwise the result will be false.
AND operator Example:
var a = 10
var b = 20
// Here both conditions are false; so result will be false
println(a == b && a > b)
// Here one condition is false; so result will be false
println(a == b && a != b)
// Here both conditions are true; so result will be true
println(a < b && a != b)
The code above outputs:
false
false
true
OR ( || ) Logical operator.
In logical operator OR or ( || ), If both conditions are false in case the result will be false otherwise the result will be true.
OR operator Example:
var a = 10
var b = 20
// Here both conditions are false; so result will be false
println(a == b || a > b)
// Here one condition is false; so result will be true
println(a == b || a != b)
// Here both conditions are true; so result will be true
println(a < b || a != b)
The code above outputs:
false
true
true
NOT ( ! ) Logical operator.
The NOT operator is pretty much simple. It reverses the result; like if the result is true this will make it false or if it is false this will make it true.
NOT operator Example:
var a = 20
var b = 20
// The condition below is true
println(a == b)
// Let's make it false by putting NOT (!) operator
println(a !== b)
The code above outputs:
true
false