The most basic operator in Kotlin is the Assignment operator =
. It is used to assign a value to variable. Example :
fun main() {
val a = 5
var b = "Hello!"
}
Depending on the type of data, we can perform several operations on it :
The most basic operator in Kotlin is the Assignment operator =
. It is used to assign a value to variable. Example :
fun main() {
val a = 5
var b = "Hello!"
}
Depending on the type of data, we can perform several operations on it :
On numerical data, we can perform arithmatic operations like :
+
, Subtraction -
, Multiplication *
, Division /
%
(calculate the reminder of a division)Examples :
fun main() {
val a = 2
val b = 6
val x = 3.14f
println("""
Arithmetic operations
$a + $b = ${a + b}
$a - $b = ${a - b}
$a * $b = ${a * b}
$a / $b = ${a / b}
$a % $b = ${a % b}
Bitwise operations
$a AND $b = ${a and b}
$a OR $b = ${a or b}
$a XOR $b = ${a xor b}
Other operations
floor($x) = ${floor(x)}
ceil($x) = ${ceil(x)}
round($x) = ${round(x)}
""".trimIndent())
}
/* Output :
Arithmetic operations
2 + 6 = 8
2 - 6 = -4
2 * 6 = 12
2 / 6 = 0
2 % 6 = 2
Bitwise operations
2 AND 6 = 2
2 OR 6 = 6
2 XOR 6 = 4
Other operations
floor(3.14) = 3.0
ceil(3.14) = 4.0
round(3.14) = 3.0
*/
While performing arithmatic operations on numbers, if operand is to be assigned the result, we can use Augmented Assignment operators.
For example, we have a variable x
and we want to multiple it by 5 and assign the result back to x
. Usually, we would do it like this :
fun main() {
var x = 10
x = x * 5;
}
But using Augmented Assignment operator, we can re-write the same operation as x *= 5
.
fun main() {
var x = 10
x *= 5; // Short form of x = x * 5
}
Note that this works for var only and not val. So, the following is invalid :
fun main() {
val x = 10
x *= 5; // ERROR!
}
Similarly, we have Augmented Assignment operator for all arithmatic operations : +=
, -=
, *=
, /=
, %=
.
To increment a variable x
by 1, we write x = x + 1
. We also have a shortcut to such increment using increment operator ++
. So, to increment x
by 1, we can write x++
. Similarly, for decrement we have decrement operator --
. So, x = x - 1
can be written as x--
.
fun main() {
var x = 1
println("""
x = ${x++}, incrementing...
x = $x
x = (incrementing...) ${++x}
x = $x
x = ${x--}, decrementing...
x = $x
x = (decrementing...) ${--x}
x = $x
""".trimIndent())
}
/* Output :
x = 1, incrementing...
x = 2
x = (incrementing...) 3
x = 3
x = 3, decrementing...
x = 2
x = (decrementing...) 1
x = 1
*/
Consider the following program :
fun main() {
println(1 / 2) // Prints "0"
}
Result of 1 / 2
is printed 0
whereas you might expect it to print 0.5
. Why is this so? Lets find out!
Here 1
& 2
are implicitly of the type Int
. So, the result of this operation is also of the type Int
.
Int /* operation */ Int = Int
Division operator when applied on Int
gives only the quotient of division and not the decimal answer. Hence we get 0
. To get result as 0.5
, we need to make at least one of the operands of floating type i.e. Float
or Double
.
fun main() {
println(1 / 2f) // Prints "0.5"
// OR
println(1f / 2) // Prints "0.5"
// OR
println(1f / 2f) // Prints "0.5"
}
To yield a Float result, it is sufficient for only one of them to be of Float type. This is because, when two different number types are used for operands, result is calculated is the type which is larger. Floating points are considered larger than Integer types :
Double, Float > Long, Int, Short, Byte
Type1 /* operation */ Type2 = Max(Type1, Type2)
// Example :
Int * Float = Float
On textual data, we can perform operations like :
Examples :
fun main() {
val firstName = "Alan"
val lastName = "Turing"
// Concatenation
val fullName = firstName + " " + lastName
println("Concatenated String = $fullName")
// Concatenation using String Template
println("Concatenated String = $firstName $lastName")
println()
// Case conversion
println("apple".uppercase())
println("BALL".lowercase())
// Substring
print("Substring = ")
println("Lady Ada Lovelace".substring(9))
}
/* Output :
Concatenated String = Alan Turing
Concatenated String = Alan Turing
APPLE
ball
Substring = Lovelace
*/
There are many more operations that can be performed on Strings. We’ll discuss about them in the String chapter.
Relational operators (can be performed on numerical, textual, boolean data & custom data type also). We get a boolean as a result of applying relational operator.
==
- Checks whether both operands are equal<, >, <=, >=
!=
- Checks whether both operands are unequalExamples :
fun main() {
val a = 5
val b = 10
val apple = "Apple"
val ball = "Ball"
println("""
On numbers :
$a == $b = ${a == b}
$a != $b = ${a != b}
$a < $b = ${a < b}
$a <= $b = ${a <= b}
$a > $b = ${a > b}
$a >= $b = ${a >= b}
On strings :
"$apple" == "$ball" = ${apple == ball}
"$apple" != "$ball" = ${apple != ball}
"$apple" < "$ball" = ${apple < ball}
"$apple" <= "$ball" = ${apple <= ball}
"$apple" > "$ball" = ${apple > ball}
"$apple" >= "$ball" = ${apple >= ball}
""".trimIndent())
}
/* Output :
On numbers :
5 == 10 = false
5 != 10 = true
5 < 10 = true
5 <= 10 = true
5 > 10 = false
5 >= 10 = false
On strings :
"Apple" == "Ball" = false
"Apple" != "Ball" = true
"Apple" < "Ball" = true
"Apple" <= "Ball" = true
"Apple" > "Ball" = false
"Apple" >= "Ball" = false
*/
On Boolean data, we can perform logical operations like
&&
- true only if both operands are true
||
- true if at least one of the operands is true
!
- Inverts the operand i.e. true -> false
, false -> true
Examples :
fun main() {
println("""
Logical AND (&&)
false && false = ${false && false}
false && true = ${false && true}
true && false = ${true && false}
true && true = ${true && true}
Logical OR (||)
false || false = ${false || false}
false || true = ${false || true}
true || false = ${true || false}
true || true = ${true || true}
Logical NOT (!)
!false = ${!false}
!true = ${!true}
""".trimIndent())
}
/* Output :
Logical AND (&&)
false && false = false
false && true = false
true && false = false
true && true = true
Logical OR (||)
false || false = false
false || true = true
true || false = true
true || true = true
Logical NOT (!)
!false = true
!true = false
*/