Introduction
In this part, we are going to learn about operators and comments in Kotlin. Those who are willing to learn about Kotlin, Pros and Cons of it, and Kotlin Variables and DataTypes, please go through my articles on the links below.
Kotlin Operators
Operators are special characters which perform an operation on values or variables.
In Kotlin, there are several kinds of operators, as mentioned below.
- Arithmetic Operator
- Relation Operator
- Assignment Operator
- Unary Operator
- Bitwise Operator
- Logical Operator
Arithmetic Operator
Arithmetic operators are used to perform the basic mathematical operations, such as addition (+), subtraction (-), multiplication (*), division (/) etc.
Operator
|
Description
|
Expression
|
+
|
Addition
|
a+b
|
-
|
Subtraction
|
a-b
|
*
|
Multiplication
|
a*b
|
/
|
Division
|
a/b
|
%
|
Modulus
|
a%b
|
Example
- fun main(args: Array<String>) {
- val number1 = 12.5
- val number2 = 3.5
- var result: Double
- result = number1 + number2
- println("number1 + number2 = $result")
- result = number1 - number2
- println("number1 - number2 = $result")
- result = number1 * number2
- println("number1 * number2 = $result")
- result = number1 / number2
- println("number1 / number2 = $result")
- result = number1 % number2
- println("number1 % number2 = $result")
- }
Output
number1 + number2 = 16.0
number1 - number2 = 9.0
number1 * number2 = 43.75
number1 / number2 = 3.57
number1 % number2 = 2.0
The +operator is also used for the concatenation of String values.
Example
Concatenation of Strings,
- fun main(args: Array<String>) {
- val start = "Abubackkar Shithik"
- val middle = "Android Developer"
- val end = "Trident Sqa"
- val result = start + middle + end
- println(result)
- }
Output
Abubackkar Shithik Android Developer Trident Sqa
Relation Operator
Relation operator shows the relation and compares between values and variables.
Operator
|
Description
|
Expression
|
<
|
Less than
|
a<b
|
>
|
Greater than
|
a>b
|
<=
|
Less than or equal to
|
a<=b
|
>=
|
Greater than or equal to
|
a>=b
|
Equal ==
|
Is equal to
|
a==b
|
!=
|
Not equal to
|
a!=b
|
Example
- fun main(args : Array<String>) {
- val a = 5
- val b = 10
- val result = if (a > b) {
- println("a is greater than b.")
- a
- } else{
- println("b is greater than a.")
- b
- }
- println("result= $result")
- }
Output
b is greater than a
result =10
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
|
+=
|
Add and assign
|
a+=b
|
-=
|
Sub and assign
|
a-=b
|
*=
|
Multiply and assign
|
a*=b
|
/=
|
Divide and assign
|
a/=b
|
%=
|
Mod and assign
|
a%=b
|
Example
- fun main(args : Array<String>) {
-
- var a =20;var b=5
- a+=b
- println("a+=b :"+ a)
- a-=b
- println("a-=b :"+ a)
- a*=b
- println("a*=b :"+ a)
- a/=b
- println("a/=b :"+ a)
- a%=b
- println("a%=b :"+ a)
- }
Output
a+=b :25
a-=b :20
a*=b :100
a/=b :20
a%=b :0
Unary Operator
A unary operator is used with only a single operand.
Operator
|
Description
|
Expression
|
+
|
Unary Plus
|
+a
|
-
|
Unary Minus
|
-a
|
++
|
Increment by 1
|
++a
|
--
|
Decrement by 1
|
--a
|
!
|
not
|
!a
|
Example
- fun main(args: Array<String>){
- var a=10
- var b=5
- var flag = true
- println("+a :"+ +a)
- println("-b :"+ -b)
- println("++a :"+ ++a)
- println("--b :"+ --b)
- println("!flag :"+ !flag)
- }
Output
+a :10
-b :-5
++a :11
--b :4
!flag :false
Bitwise Operator
Unlike Java, there are no bitwise and bitshift operators in Kotlin. To perform these tasks, various functions (supporting infix notation) are there.
Named Function
|
Description
|
Expression
|
shl
|
Signed Shift Left
|
a.shl(b)
|
shr
|
Signed Shift Right
|
a.shr(b)
|
ushr
|
Unsigned Shift Right
|
a.ushr(b)
|
and
|
Bitwise and
|
a.and(b)
|
or
|
Bitwise or
|
a.or(b)
|
xor
|
Bitwise xor
|
a.xor(b)
|
inv
|
Bitwise inverse
|
a.inv()
|
Example
- fun main(args: Array<String>){
- var a=10
- var b=2
-
- println("a.shl(b): "+a.shl(b))
- println("a.shr(b): "+a.shr(b))
- println("a.ushr(b:) "+a.ushr(b))
- println("a.and(b): "+a.and(b))
- println("a.or(b): "+a.or(b))
- println("a.xor(b): "+a.xor(b))
- println("a.inv(): "+a.inv())
- }
Output
a.shl(b): 40
a.shr(b): 2
a.ushr(b:) 2
a.and(b): 2
a.or(b): 10
a.xor(b): 8
a.inv(): -11
Logical Operators
There are three logical operators in Kotlin: Or, And, and Not denoted by ||, &&, ! respectively.
Operator
|
Description
|
Expression
|
||
|
Return true if any expression are true
|
(a>b)||(a>c)
|
&&
|
Return true if all expression are true
|
(a>b)&&(a>c)
|
!
|
Return Compliment of expression
|
a.not()
|
Example
- fun main(args: Array<String>){
- var a=10
- var b=5
- var c=15
- var flag = false
- var result: Boolean
- result = (a>b) && (a>c)
- println("(a>b) && (a>c) :"+ result)
- result = (a>b) || (a>c)
- println("(a>b) || (a>c) :"+ result)
- result = !flag
- println("!flag :"+ result)
- }
Output
(a>b) && (a>c) :false
(a>b) || (a>c) :true
!flag :true
Kotlin – Comment
Comments are the statements that are used for documentation purposes. Comments are ignored by the compiler so that they don't get executed. We can also use it for providing information about the line of code. There are two types of comments in Kotlin.
- Single line comment.
- Multi-line comment.
Single line comment
Single line comment is used for commenting a single line of the statement. It is done by using '//' (double slash).
Multi-line comment
Multi-line comment is used for commenting multiple lines of a statement. It is done by using /* */ (start with slash strict and end with star slash).
Conclusion
In this article, we learned about operators and comments in Kotlin. In the next part, we will learn about Control Expressions, Return, and Jump.