Kotlin - Operators And Comments

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.
 
Kotlin- Operator
 
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 
  1. fun main(args: Array<String>) {  
  2.     val number1 = 12.5  
  3.     val number2 = 3.5  
  4.     var result: Double  
  5.     result = number1 + number2  
  6.     println("number1 + number2 = $result")  
  7.     result = number1 - number2  
  8.     println("number1 - number2 = $result")  
  9.     result = number1 * number2  
  10.     println("number1 * number2 = $result")  
  11.     result = number1 / number2  
  12.     println("number1 / number2 = $result")  
  13.     result = number1 % number2  
  14.     println("number1 % number2 = $result")  
  15. }  
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,
  1. fun main(args: Array<String>) {  
  2.     val start = "Abubackkar Shithik"  
  3.     val middle = "Android Developer"  
  4.     val end = "Trident Sqa"  
  5.     val result = start + middle + end  
  6.     println(result)  
  7. }  
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 
  1. fun main(args : Array<String>) {    
  2.     val a = 5    
  3.     val b = 10    
  4.     val result = if (a > b) {    
  5.         println("a is greater than b.")    
  6.         a    
  7.     } else{    
  8.         println("b is greater than a.")    
  9.         b    
  10.     }    
  11.     println("result= $result")    
  12. }    
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
  1. fun main(args : Array<String>) {    
  2.     
  3.     var a =20;var b=5    
  4.     a+=b    
  5.     println("a+=b :"+ a)    
  6.     a-=b    
  7.     println("a-=b :"+ a)    
  8.     a*=b    
  9.     println("a*=b :"+ a)    
  10.     a/=b    
  11.     println("a/=b :"+ a)    
  12.     a%=b    
  13.     println("a%=b :"+ a)    
  14. }    
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 
  1. fun main(args: Array<String>){    
  2.     var a=10    
  3.     var b=5    
  4.     var flag = true    
  5.     println("+a :"+ +a)    
  6.     println("-b :"+ -b)    
  7.     println("++a :"+ ++a)    
  8.     println("--b :"+ --b)    
  9.     println("!flag :"+ !flag)    
  10. }    
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
  1. fun main(args: Array<String>){    
  2.     var a=10    
  3.     var b=2    
  4.     
  5.     println("a.shl(b): "+a.shl(b))    
  6.     println("a.shr(b): "+a.shr(b))    
  7.     println("a.ushr(b:) "+a.ushr(b))    
  8.     println("a.and(b): "+a.and(b))    
  9.     println("a.or(b): "+a.or(b))    
  10.     println("a.xor(b): "+a.xor(b))    
  11.     println("a.inv(): "+a.inv())     
  12. }  
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
  1. fun main(args: Array<String>){    
  2.     var a=10    
  3.     var b=5    
  4.     var c=15    
  5.     var flag = false    
  6.     var result: Boolean    
  7.     result = (a>b) && (a>c)    
  8.     println("(a>b) && (a>c) :"+ result)    
  9.     result = (a>b) || (a>c)    
  10.     println("(a>b) || (a>c) :"+ result)    
  11.     result = !flag    
  12.     println("!flag :"+ result)    
  13. }   
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.
 
 
 


Similar Articles