Kotlin Operators
Operators are used to performing operations on variables and values.
They are various kinds of operators available in Kotlin.
- Arithmetic operator
- Relation operator
- Assignment operator
- Unary operator
- Bitwise operation
- Logical operator
Arithmetic Operator
Arithmetic operators are used to perform basic mathematical operations such as addition (+), subtraction (-), multiplication (*), division (/), etc.
Operator | Description | Expression | Translate to |
---|---|---|---|
+ | Addition | a+b | a.plus(b) |
- | Subtraction | a-b | a.minus(b) |
* | Multiply | a*b | a.times(b) |
/ | Division | a/b | a.div(b) |
% | Modulus | a%b | a.rem(b) |
Relation Operator
The relation operator shows the relation and compares between operands. Following are the different relational operators:
Operator | Description | Expression | Translate to |
---|---|---|---|
> | greater than | a>b | a.compateTo(b)>0 |
< | Less than | a<b | a.compateTo(b)<0 |
>= | greater than or equal to | a>=b | a.compateTo(b)>=0 |
<= | less than or equal to | a<=b | a?.equals(b)?:(b===null) |
== | is equal to | a==b | a?.equals(b)?:(b===null) |
!= | not equal to | a!=b | !(a?.equals(b)?:(b===null)) |
Assignment operator
Assignment operator "=" is used to assign a value to another variable. The assignment of value takes from right to left.
Operator | Description | Expression | Convert to |
---|---|---|---|
+= | add and assign | a+=b | a.plusAssign(b) |
-= | subtract and assign | a-=b | a.minusAssign(b) |
*= | multiply and assign | a*=b | a.timesAssign(b) |
/= | divide and assign | a/=b | a.divAssign(b) |
%= | mod and assign | a%=b | a.remAssign(b) |
Unary Operator
A unary operator is used with only a single operand. Following are some unary operators given below.
Operator | Description | Expression | Convert to |
---|---|---|---|
+ | unary plus | +a | a.unaryPlus() |
- | unary minus | -a | a.unaryMinus() |
++ | increment by 1 | ++a | a.inc() |
-- | decrement by 1 | --a | a.dec() |
! | not | !a | a.not() |
Logical Operator
Logical operators are used to checking conditions between operands. A list of logical operators is given below.
Operator | Description | Expression | Convert to |
---|---|---|---|
&& | return true if all expression are true | (a>b) && (a>c) | (a>b) and (a>c) |
|| | return true if any expression are true | (a>b) || (a>c) | (a>b) or(a>c) |
! | return complement of expression | !a | a.not() |
Bitwise Operation
In Kotlin, there is not any special bitwise operator. Bitwise operation is done using the named function.
Named Function | Description | Expression |
---|---|---|
shl (bits) | signed shift left | a.shl(b) |
shr (bits) | signed shift right | a.shr(b) |
ushr (bits) | unsigned shift right | a.ushr(b) |
and (bits) | bitwise and | a.and(b) |
or (bits) | bitwise or | a.or(b) |
xor (bits) | bitwise xor | a.xor(b) |
inv() | bitwise inverse | a.inv() |
fun main(){
//Arithmetic operators (+, -, *, /, %)
var result = 5+3
result = result / 2
// alternatively
// result /= 2
result = result * 5
result = result - 1
var moduloResult = 5%2
println( moduloResult)
//Comparison operators (==, !=, <, >, <=, >=)
val isEqual = 5==3
// Concatenation - adding of "Strings"
println("isEqual is " + isEqual)
val isNotEqual = 5!=5
// Kotlin has a feature called String Interpolation.
// This feature allows you to directly insert a template expression inside a String.
// Template expressions are tiny pieces of code that are evaluated and
// their results are concatenated with the original String.
// A template expression is prefixed with $ symbol.
// Following are examples of String interpolation
println("isNotEqual is $isNotEqual")
println("is5Greater3 ${5 > 3}")
println("is5LowerEqual3 ${5 >= 3}")
println("is5LowerEqual5 ${5 >= 5}")
//Assignment operators (+=, -=, *=, /=, %=)
var myNum = 5
myNum += 3
println("myNum is $myNum")
myNum *= 4
println("myNum is $myNum")
//Increment & Decrement operators (++, --)
myNum++
println("myNum is $myNum")
// increments after use
println("myNum is ${myNum++}")
// increments before use
println("myNum is ${++myNum}")
println("myNum is ${--myNum}")
}
..
Comments
Post a Comment