Kotlin Operators
$count++; if($count == 1) { include "../mobilemenu.php"; } if ($count == 2) { include "../sharemediasubfolder.php"; } ?>
Kotlin Operators:
Listed below are various operators available in Kotlin.
A brief description is provided with the operators.
postfix operators:
i++: increments the value of i by 1. Value is returned first and then incremented.
i--: decrements the value of i by 1. Value is returned first and then decremented.
prefix operators:
++i: increments the value of i by 1. Value is incremented first and then returned.
--i: decrements the value of i by 1. Value is decremented first and then returned.
fun main(args: Array<String>)
{
var i: Int = 0
println("i++: " + i++)
println("++i: " + ++i)
}
Output:
i++: 0
++i: 2
fun main(args: Array<String>)
{
var i: Int = 0
println("i--: " + i--)
println("--i: " + --i)
}
Output:
i--: 0
--i: -2
+i : Positive value representation of the variable i.
-i : Negative value representation of the variable i.
inv : Bitwise complement. it flips the bit.
! : logical NOT operator. it flips the logical value from false to true and true to false.
fun main(args: Array<String>)
{
var i: Int = 23
println("+i: " + +i)
println("-i: " + -i)
println("~i: " + i.inv())
var iLie: Boolean = true
println("!iLie: " + !iLie)
}
Output:
+i: 23
-i: -23
~i: -24
!iLie: false
* : It multiples the left and right operand and returns the result.
% : Modulo operator. Divides the left operand with the right operand and returns the remainder.
/ : Division Operator. Divides the left operand with the right operand and returns the result.
fun main(args: Array<String>)
{
var i: Int = 24
println("i * 2: " + i * 2)
println("i / 3: " + i / 3)
println("i % 5: " + i % 5)
}
Output:
i * 2: 48
i / 3: 8
i % 5: 4
+ : Addition operator. Adds the left operand with the right operand and returns the result.
- : Subtraction operator. Subtracts the left operand and right operand and returns the result.
fun main(args: Array<String>)
{
var i: Int = 24
var j: Int = 12
println("i + j: " + i + j)
println("i - j: " + (i - j))
}
Output:
i + j: 2412
i - j: 12
=== : Check referential equality operator.
!== : Check referential inequality operator.
fun main(args: Array<String>)
{
var i: Int = 24
var j: Int = 24
println("i === j: " + (i === j))
println("i !== j: " + (i !== j))
}
Output:
i === j: true
i !== j: false
< : Less than operator. Check if the left operand is less than the right operand. Return true or false.
> : Greater than operator. Check if the left operand is greater than the right operand. Return true or false.
<= : Less than or equal to. Check if the left operand is less OR equal to the right operand. Return true or false.
>= : Greater than or equal to. Check if the left operand is greater than or equal to the right operand. Return true or false.
fun main(args: Array<String>)
{
var i: Int = 24
var j: Int = 12
println("i < j: " + (i < j))
println("i > j: " + (i > j))
println("i >= j: " + (i >= j))
println("i <= j: " + (i <= j))
}
Output:
i < j: false
i > j: true
i >= j: true
i <= j: false
== : Check if left operand is equal to the right operand. Return true or false.
!= : Check if left operand is not equal to the right operand. Return true or false.
fun main(args: Array<String>)
{
var i: Int = 24
var j: Int = 24
println("i == j: " + (i == j))
println("i != j: " + (i != j))
}
Output:
i == j: true
i != j: false
and : Bitwise AND Operator. If bit exists in both left and right operand, copy the bit to the result.
bitwise exclusive OR:
xor : Bitwise XOR Operator. If bit is set in either left or right operand but not in both, copy the bit to the result.
bitwise inclusive OR:
or : Bitwise OR Operator. If bit exists in either left or right operand, copy the bit to the result.
fun main(args: Array<String>)
{
var i: Boolean = true
var j: Boolean = false
println("i or j: " + (i or j))
println("i and j: " + (i and j))
println("i xor j: " + (i xor j))
}
Output:
i or j: true
i and j: false
i xor j: true
&& : Logical AND Operator. If both left and right operand is true, the result will be true else false.
logical OR:
|| : Logical OR Operator. If Either left or right operand is true, the result will be true else false.
fun main(args: Array<String>)
{
var i: Boolean = true
var j: Boolean = false
println("i && j: " + (i && j))
println("i || j: " + (i || j))
}
Output:
i && j: false
i || j: true
= : Assignment operator: Assign the right operand to the left operand.
+= : Add plus assignment operator. Adds the left and right operand AND assigns the result to the left operand.
-= : Subtract plus assignment operator. Subtracts the left and right operand AND assigns the result to the left operand.
*= : Multiply plus assignment operator. Multiplies the left and right operand AND assigns the result to the left operand.
/= : Divide plus assignment operator. Divides the left and right operand AND assigns the result to the left operand.
%= : Modulo plus assignment operator. Divides the left and right operand AND assigns the remainder to the left operand.
fun main(args: Array<String>)
{
var j: Int = 5
j += 5
println("j += 5: " + j)
j -= 2
println("j -= 2: " + j)
j *= 3
println("j *= 3: " + j)
j /= 8
println("j /= 8: " + j)
}
Output:
j += 5: 10
j -= 2: 8
j *= 3: 24
j /= 8: 3
[] : Index access operator.
!! : Assertion not null operator.
$ : String templates expression operator.
?. : Null check call operator. Call property only if target is not null.
.. : Range operator.
:: : Create a class reference.
: : Name type separator.
-> : lambda operator.
? : Mark type as null.
@ : annotation, loop reference, lambda reference operator.
; : Statement separator.