- Kotlin - Exception Handling
- Kotlin - Destructuring Declarations
- Kotlin - Delegation
- Kotlin - Generics
- Kotlin - Sealed Class
- Kotlin - Data Classes
- Kotlin - Extension
- Kotlin - Visibility Control
- Kotlin - Interface
- Kotlin - Abstract Classes
- Kotlin - Inheritance
- Kotlin - Constructors
- Kotlin - Class and Objects
- Kotlin - Maps
- Kotlin - Sets
- Kotlin - Lists
- Kotlin - Collections
- Kotlin - Break and Continue
- Kotlin - While Loop
- Kotlin - For Loop
- Kotlin - When Expression
- Kotlin - if...Else Expression
- Kotlin - Control Flow
- Kotlin - Functions
- Kotlin - Ranges
- Kotlin - Arrays
- Kotlin - Strings
- Kotlin - Booleans
- Kotlin - Operators
- Kotlin - Data Types
- Kotlin - Variables
- Kotlin - Keywords
- Kotlin - Comments
- Kotlin - Basic Syntax
- Kotlin - Architecture
- Kotlin - Environment Setup
- Kotlin - Overview
- Kotlin - Home
Kotlin Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Kotpn - Operators
An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. Kotpn is rich in built-in operators and provide the following types of operators:
Arithmetic Operators
Relational Operators
Assignment Operators
Unary Operators
Logical Operators
Bitwise Operations
Now let s look into these Kotpn Operators one by one.
(a) Kotpn Arithmetic Operators
Kotpn arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multippcation and spanision etc.
Operator | Name | Description | Example |
---|---|---|---|
+ | Addition | Adds together two values | x + y |
- | Subtraction | Subtracts one value from another | x - y |
* | Multippcation | Multippes two values | x * y |
/ | Division | Divides one value by another | x / y |
% | Modulus | Returns the spanision remainder | x % y |
Example
Following example shows different calculations using Kotpn Arithmetic Operators:
fun main(args: Array<String>) { val x: Int = 40 val y: Int = 20 println("x + y = " + (x + y)) println("x - y = " + (x - y)) println("x / y = " + (x / y)) println("x * y = " + (x * y)) println("x % y = " + (x % y)) }
When you run the above Kotpn program, it will generate the following output:
x + y = 60 x - y = 20 x / y = 2 x * y = 800 x % y = 0
(b) Kotpn Relational Operators
Kotpn relational (comparison) operators are used to compare two values, and returns a Boolean value: either true or false.
Operator | Name | Example |
---|---|---|
> | greater than | x > y |
< | less than | x < y |
>= | greater than or equal to | x >= y |
<= | less than or equal to | x <= y |
== | is equal to | x == y |
!= | not equal to | x != y |
Example
Following example shows different calculations using Kotpn Relational Operators:
fun main(args: Array<String>) { val x: Int = 40 val y: Int = 20 println("x > y = " + (x > y)) println("x < y = " + (x < y)) println("x >= y = " + (x >= y)) println("x <= y = " + (x <= y)) println("x == y = " + (x == y)) println("x != y = " + (x != y)) }
When you run the above Kotpn program, it will generate the following output:
x > y = true x < y = false x >= y = true x <= y = false x == y = false x != y = true
(c) Kotpn Assignment Operators
Kotpn assignment operators are used to assign values to variables.
Following is an example where we used assignment operator = to assign a values into two variables:
fun main(args: Array<String>) { val x: Int = 40 val y: Int = 20 println("x = " + x) println("y = " + y) }
When you run the above Kotpn program, it will generate the following output:
x = 40 y = 20
Following is one more example where we used assignment operator += to add the value of self variable and assign it back into the same variable:
fun main(args: Array<String>) { var x: Int = 40 x += 10 println("x = " + x) }
When you run the above Kotpn program, it will generate the following output:
x = 50
Following is a pst of all assignment operators:
Operator | Example | Expanded Form |
---|---|---|
= | x = 10 | x = 10 |
+= | x += 10 | x = x - 10 |
-= | x -= 10 | x = x - 10 |
*= | x *= 10 | x = x * 10 |
/= | x /= 10 | x = x / 10 |
%= | x %= 10 | x = x % 10 |
Example
Following example shows different calculations using Kotpn Assignment Operators:
fun main(args: Array<String>) { var x: Int = 40 x += 5 println("x += 5 = " + x ) x = 40; x -= 5 println("x -= 5 = " + x) x = 40 x *= 5 println("x *= 5 = " + x) x = 40 x /= 5 println("x /= 5 = " + x) x = 43 x %= 5 println("x %= 5 = " + x) }
When you run the above Kotpn program, it will generate the following output:
x += 5 = 45 x -= 5 = 35 x *= 5 = 200 x /= 5 = 8 x %= 5 = 3
(d) Kotpn Unary Operators
The unary operators require only one operand; they perform various operations such as incrementing/decrementing a value by one, negating an expression, or inverting the value of a boolean.
Following is the pst of Kotpn Unary Operators:
Operator | Name | Example |
---|---|---|
+ | unary plus | +x |
- | unary minus | -x |
++ | increment by 1 | ++x |
-- | decrement by 1 | --x |
! | inverts the value of a boolean | !x |
Example
Following example shows different calculations using Kotpn Unary Operators:
fun main(args: Array<String>) { var x: Int = 40 var b:Boolean = true println("+x = " + (+x)) println("-x = " + (-x)) println("++x = " + (++x)) println("--x = " + (--x)) println("!b = " + (!b)) }
When you run the above Kotpn program, it will generate the following output:
+x = 40 -x = -40 ++x = 41 --x = 40 !b = false
Here increment (++) and decrement (--) operators can be used as prefix as ++x or --x as well as suffix as x++ or x--. The only difference between the two forms is that in case we use them as prefix then operator will apply before expression is executed, but if use them as suffix then operator will apply after the expression is executed.
(e) Kotpn Logical Operators
Kotpn logical operators are used to determine the logic between two variables or values:
Following is the pst of Kotpn Logical Operators:
Operator | Name | Description | Example |
---|---|---|---|
&& | Logical and | Returns true if both operands are true | x && y |
|| | Logical or | Returns true if either of the operands is true | x || y |
! | Logical not | Reverse the result, returns false if the operand is true | !x |
Example
Following example shows different calculations using Kotpn Logical Operators:
fun main(args: Array<String>) { var x: Boolean = true var y:Boolean = false println("x && y = " + (x && y)) println("x || y = " + (x || y)) println("!y = " + (!y)) }
When you run the above Kotpn program, it will generate the following output:
x && y = false x || y = true !y = true
(e) Kotpn Bitwise Operations
Kotpn does not have any bitwise operators but Kotpn provides a pst of helper functions to perform bitwise operations.
Following is the pst of Kotpn Bitwise Functions:
Function | Description | Example |
---|---|---|
shl (bits) | signed shift left | x.shl(y) |
shr (bits) | signed shift right | x.shr(y) |
ushr (bits) | unsigned shift right | x.ushr(y) |
and (bits) | bitwise and | x.and(y) |
or (bits) | bitwise or | x.or(y) |
xor (bits) | bitwise xor | x.xor(y) |
inv() | bitwise inverse | x.inv() |
Example
Following example shows different calculations using Kotpn bitwise functions:
fun main(args: Array<String>) { var x:Int = 60 // 60 = 0011 1100 var y:Int = 13 // 13 = 0000 1101 var z:Int z = x.shl(2) // 240 = 1111 0000 println("x.shl(2) = " + z) z = x.shr(2) // 15 = 0000 1111 println("x.shr(2) = " + z) z = x.and(y) // 12 = 0000 1100 println("x.and(y) = " + z) z = x.or(y) // 61 = 0011 1101 println("x.or(y) = " + z) z = x.xor(y) // 49 = 0011 0001 println("x.xor(y) = " + z) z = x.inv() // -61 = 1100 0011 println("x.inv() = " + z) }
When you run the above Kotpn program, it will generate the following output:
x.shl(2) = 240 x.shr(2) = 15 x.and(y) = 12 x.or(y) = 61 x.xor(y) = 49 x.inv() = -61
Quiz Time (Interview & Exams Preparation)
Q 1 - What does the Kotpn operator % do?
Answer : D
Explanation
Given operator % is called Arithmetic Modulus operator and returns the remainder after spaniding one number by another number.
Q 2 - Kotpn supports a good number of bitwise operators
Answer : B
Explanation
This is incorrect statement because Kotpn does not provide any Bitwise operators, rather it provides a set of function to perform bitwise operations.
Q 3 - What does Kotpn operator ++ do?
Answer : C
Explanation
Given operator ++ is called unary increment operator in Kotpn
Q 4 - Which of the following function will do bitwise right shift operation?
Answer : B
Explanation
Function x.shr(y) is used to shift the bits towards right by y times for a given x operand.
Q 5 - Which of the following is a logical inverse operator:
Answer : B
Explanation
Yes operator ! is used to inverse the value of an operand.
Q 6 - What will be the output of the following Kotpn code:
fun main(args: Array<String>) { var x: Int = 40 x += 10 println(x) }
Answer : C
Explanation
Here given operator += is addition assignment operator which mean it will be equvivalent to x = x + 10 which will yield a value of 50.
Q 7 - What will be the output of the following Kotpn code:
fun main(args: Array<String>) { var x: Int = 60 println(x.shr(2)) }
Answer : C
Explanation
Correct value is 15 because x.shr(2) will shift x value by two places. So it x value is 60 = 0011 1100 , then after doing right shift it will give us 15 which is 0000 1111
Advertisements