Skip to main content

Kotlin Operator

Kotlin Operator
Operators are special characters which perform operation on operands (values or variable).There are various kind 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.
 
+ 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)


Example of Arithmetic Operator

fun main(args : Array) {
    var a=10;
    var b=5;
    println(a+b);
    println(a-b);
    println(a*b);
    println(a/b);
    println(a%b);
}


Relation Operator
Relation operator shows the relation and compares between operands. Following are the different relational operators:
> greater than a>b a.compateTo(b)>0
< Less than a >= 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))

Example of Relation Operator

fun main(args : Array) {
    val a = 5    
    val b = 10
    val max = if (a > b) {
        println("a is greater than b.")
        a
    } else{
        println("b is greater than a.")
        b
    }
        println("max = $max")
    }

Assignment operator
Assignment operator "=" is used to assign a value to another variable. The assignment of value takes from right to left.

+= 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)

Example of Assignment operator

fun main(args : Array) {
    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)
}

Unary Operator
Unary operator is used with only single operand. Following are some unary operator given below.

+ 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()

fun main(args: Array){
    var a=10
    var b=5    
    var flag = true
    println("+a :"+ +a)
    println("-b :"+ -b)
    println("++a :"+ ++a)
    println("--b :"+ --b)
    println("!flag :"+ !flag)
}

Logical Operator
Logical operators are used to check conditions between operands. List of logical operators are given below.
&& 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()

fun main(args: Array){
    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)
}

(a>b) && (a>c) :false
(a>b) || (a>c) :true
!flag :true

Bitwise Operation
In Kotlin, there is not any special bitwise operator. Bitwise operation is done using named function.
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()

Example of Bitwise Operation

fun main(args: Array
){
    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())
}

Comments

Popular posts from this blog

BCA SEM 5 SHELL SCRIPT PROGRAM

1  Write a shell script to execute following commands 1. Sort file abc.txt and save this sorted file in xyz.txt 2. Give an example of : To execute commands together without affecting result of each other. 3. How to print “this is a three –line 1. Text message” 4. Which command display version of the UNIX? 5. How would u get online help of cat command? echo “sorting the file” sort abc.txt > xyz.txt echo “executing two commands” who ; ls echo “this is \n a three-line \n Text message” # use -e option if required echo “The version is `uname -a`” echo “Help of cat command” man cat 2  Write a shell script to execute following commands 1. How would u display the hidden files? 2. How delete directory with files? 3. How would user can do interactive copying? 4. How would user can do interactive deletion of files? 5. Explain two functionality of “mv” command with example? echo “1. How would u display the hidden files” echo “2. How delete directory with files” echo “3. How

C++

I NTRODUCTION TO OOP,CLASSES & OBJECTS 1. Use of scope Resolution of Operators. 2. Define a function outside a using scope resolution operators. 3. Write a program to calculate the area of circle, rectangle and square using function overloading. 4. Write a program to calculate the area of circle, rectangle and square using with class & object. 5. Write a program to demonstrate the use of returning a reference variable. 6. Create a class student,stores the details about name,roll no,marks of 5 subject,1.get function accept value of data members,2. display function to display,3.total function to return total of 5 subjects marks. 7. Create function power() in c++. & Create function power() in c++ and default argument. 8. Write a C++ program to swap the value of private data members from 2 different classes. 9. Write a program to illustrate the use of this pointer. 10. An election is contested by five candidates. The candidates are numbered 1 to 5 and the voting is do

USER- DEFINED FUNCTIONS

1. Write a program to calculate average temperature of five days.Create temp() function . 2. Write a program that uses recursive function fibo() thatgenerates a Fibonacci series containing N elements. 3. Write a program that uses a recursive function fact() that findsthe factorial of a given number N. 4. Program to find if the given no. is prime or not. The functionshould accept the number as argument and return if the no. isprime or not. 5. Write a function that accepts an array of integer values. Thefunction should find the number which divides all the othernumbers. 6. Write a user-defined function to perform Square of a number 7. Write a user-defined function to perform Area of a number 8. Write a user-defined function to perform Reverse the number 9. Write a program that uses a function to check whether anentered three digit number is palindrome or not. 10. Write a program to calculate the result of following with recursive calls offunction. 11. Simple interset with usin