Kotlin Operators
Operators are the special symbols that perform different operation on operands. For example + and – are operators that perform addition and subtraction respectively. Like Java, Kotlin contains different kinds of operators.
- Arithmetic operator
- Relation operator
- Assignment operator
- Unary operator
- Logical operator
- Bitwise operator
Arithmetic Operators –
Operators | Meaning | Expression | Translate to |
---|---|---|---|
+ | Addition | a + b | a.plus(b) |
– | Subtraction | a – b | a.minus(b) |
* | Multiplication | a * b | a.times(b) |
/ | Division | a / b | a.div(b) |
% | Modulus | a % b | a.rem(b) |
Kotlin
fun main(args: Array<String>) { var a = 20 var b = 4 println( "a + b = " + (a + b)) println( "a - b = " + (a - b)) println( "a * b = " + (a.times(b))) println( "a / b = " + (a / b)) println( "a % b = " + (a.rem(b))) } |
Output:
a + b = 24 a - b = 16 a * b = 80 a / b = 5 a % b = 0
Relational Operators –
Operators | Meaning | Expression | Translate to |
---|---|---|---|
> | greater than | a > b | a.compareTo(b) > 0 |
< | less than | a < b | a.compareTo(b) < 0 |
>= | greater than or equal to | a >= b | a.compareTo(b) >= 0 |
<= | less than or equal to | a <= b | a.compareTo(b) <= 0 |
== | is equal to | a == b | a?.equals(b) ?: (b === null) |
!= | not equal to | a != b | !(a?.equals(b) ?: (b === null)) > 0 |
Kotlin
fun main(args: Array<String>) { var c = 30 var d = 40 println( "c > d = " +(c>d)) println( "c < d = " +(c.compareTo(d) < 0 )) println( "c >= d = " +(c>=d)) println( "c <= d = " +(c.compareTo(d) <= 0 )) println( "c == d = " +(c==d)) println( "c != d = " +(!(c?.equals(d) ?: (d === null )))) } |
Output:
c > d = false c < d = true c >= d = false c <= d = true c == d = false c != d = true
Assignment Operators –
Operators | Expression | Translate to |
---|---|---|
+= | a = a + b | a.plusAssign(b) > 0 |
-= | a = a – b | a.minusAssign(b) < 0 |
*= | a = a * b | a.timesAssign(b)>= 0 |
/= | a = a / b | a.divAssign(b) <= 0 |
%= | a = a % b | a.remAssign(b) |
Kotlin
fun main(args : Array<String>){ var a = 10 var b = 5 a+=b println(a) a-=b println(a) a*=b println(a) a/=b println(a) a%=b println(a) } |
Output:
15 10 50 10 0
Unary Operators –
Operators | Expression | Translate to |
---|---|---|
++ | ++a or a++ | a.inc() |
— | –a or a– | a.dec() |
Kotlin
fun main(args : Array<String>){ var e= 10 var flag = true println( "First print then increment: " + e++) println( "First increment then print: " + ++e) println( "First print then decrement: " + e--) println( "First decrement then print: " + --e) } |
Output:
First print then increment: 10 First increment then print: 12 First print then decrement: 12 First decrement then print: 10
Logical Operators –
Operators | Meaning | Expression |
---|---|---|
&& | return true if all expressions are true | (a>b) && (a>c) |
|| | return true if any of expression is true | (a>b) || (a>c) |
! | return complement of the expression | a.not() |
Kotlin
fun main(args : Array<String>){ var x = 100 var y = 25 var z = 10 var result = false if (x > y && x > z) println(x) if (x < y || x > z) println(y) if ( result.not()) println( "Logical operators" ) } |
Output:
100 25 Logical operators
Bitwise Operators –
Operators | Meaning | Expression |
---|---|---|
shl | signed shift left | a.shl(b) |
shr | signed shift right | a.shr(b) |
ushr | unsigned shift right | a.ushr() |
and | bitwise and | a.and(b) |
or | bitwise or | a.or() |
xor | bitwise xor | a.xor() |
inv | bitwise inverse | a.inv() |
Kotlin
fun main(args: Array<String>) { println( "5 signed shift left by 1 bit: " + 5 .shl( 1 )) println( "10 signed shift right by 2 bits: : " + 10 .shr( 2 )) println( "12 unsigned shift right by 2 bits: " + 12 .ushr( 2 )) println( "36 bitwise and 22: " + 36 .and( 22 )) println( "36 bitwise or 22: " + 36 .or( 22 )) println( "36 bitwise xor 22: " + 36 .xor( 22 )) println( "14 bitwise inverse is: " + 14 .inv()) } |
Output:
5 signed shift left by 1 bit: 10 10 signed shift right by 2 bits: : 2 12 unsigned shift right by 2 bits: 3 36 bitwise and 22: 4 36 bitwise or 22: 54 36 bitwise xor 22: 50 14 bitwise inverse is: -15