   ## Operators in Kotlin

What is Operator in Kotlin ?

Operators are functions which uses symbolic name representation in kotlin, Kotlin has several built-in operators which are indirectly nothing but function calls.

Operators present in Kotlin :

• Arithmetic operators : Mathematic calculations
• Comparison operators : compare the values and objects
• Assignment operators : define values
• Logical Operators : combine comparison Operator expressions
• Unary Operators : Increment / Decrement Operators
• Equality Operators : Compares the reference
• in Operator : check object is a sub in a bigger object
• is Operator : check for same type and Cast
• index Operator : get the sub-object at an index in the Bigger object collection
• invoke Operator : override invoke operator
• Bitwise Operation :

## Arithmetic operators in Kotlin

Arithmetic operators help in calculation mathematical result of operations between the values. Arithmetic operations in kotlin take two operands and perform a calculation on them.

All the Arithmetic Operators take two values to perform any operation.

• value1 + value2 : + adds two given values and returns the result, it is internally calls value1.plus(value2).
• value1 - value2 : - subtracts the value2 from the value1, this function internally calls value1.minus(value2)
• value1 * value2 : value1 will be multiplied by value2, internally calls value1.times(value2)
• value1 / value2 : value1 will be divided by value2, internally calls value1.div(value2)
• value1 % value2 : value1 will be divided by value2, and this method return the remainder, this internally calls value1.rem(value2)
• value1..value2 : it will generate the numbers from the value1 to value2, this is range function and internally it calls value1.rangeTo(value2)
```fun main(args:Array<String>)
{
var a = 9
var b = 2
println("Using Operators**********************")
println("The addition value is : " +(a+b))
println("The subtraction value is : " +(a-b))
println("The multiply value is : " +(a*b))
println("The divided value is : " +(a/b))
println("The remainder value is : " +(a%b))
println("
Using Internal Functions************")
println("The addition value is : " +(a.plus(b)))
println("The subtraction value is : " +(a.minus(b)))
println("The multiply value is : " +(a.times(b)))
println("The divided value is : " +(a.div(b)))
println("The remainder value is : " +(a.rem(b)))
}

```

## Comparison Operators in Kotlin

Comparison Operators simply compares the values of the variables or objects, we can also compare the reference of the objects.

All below operators compares value1 and value2

• value1 > value2 : if value1 is greater than value2 then > operator returns true, calls a.compareTo(b) > 0
• value1 < value2 : if value1 is less than value2 then < operator returns true, calls a.compareTo(b) < 0
• value1 >= value2 : if value1 is greater than or equal to value2 then >= operator returns true, calls a.compareTo(b) >= 0
• value1 <= value2 : if value1 is lesser than or equal to value2 then <= operator returns true, calls a.compareTo(b) <= 0
• value1 == value2 : value1 is equal to value2
When using compareTo you have to override the method in the comparable interface
• To Indicate a is less than b, it returns a negative integer
• When b is greater than a, it returns a positive integer
• For equality, it returns 0

## Assignment Operators in Kotlin

The Assignment operator ('=') is used to assign values to the variables, Assignment operators are used to assigning value to a variable.

In the below code, we are assigning karthiq to name.

`var name = "karthiq"`

We can use the arithmetic operators along with the assignment operators, Below example, shows only for addition operation but you can understand the operation.

`a += b  => a = a + b`
```fun main(args:Array<String>)
{
var a = 9
var b = 2
println("Using Arithmetic Operator along with Assignment")
a += b
println("The addition value is : " +a)
}```

## Logical Operators in Kotlin

Logical Operators also called as boolean operators, Logical Operators result in boolean values based on the boolean expressions.

There are two logical operators in Kotlin: || and &&, logical operators accept two values. || true if either of the Boolean expression is true

&& true if all Boolean expressions are true At least one value is true then || operator will return true

```OR Operator
true || true => true
true || false => true
false || true => true
false || false => false```

Bot value must be true for && operator will return true otherwise false

```AND Operator
true && true => true
true && false => true
false && true => true
false && false => false```

You can combine both operators in the same expression, it is better to surround the unit logic with ()

```var a =false
var b = true
(a || b) && (a&&b)
```

## Unary Operators in Kotlin

Unary Operators takes one value and perform increment or decrement operation on it, These operators will be translated into a method, which I have showcased with =>

+ plus operator just makes the value into a positive value.

`+a => a.unaryPlus()`

- minus operator makes the value into a positive value.

`-a => a.unaryMinus()`

! operator makes a value into other than that value most of the time it is used along with boolean values

`!a => a.not()`

++, -- will have permanent effects, that means when increase or decrease the value and the value will be stored to the same variable after the changes

++ operator increments the value by 1, if ++ is prefixed that means before reading the value ++ operator adds 1 to the un-read value. (Example, ++15 will be 16 in the same line)

if ++ is post-fixed that means after reading the value ++ operator adds 1 to the read value and it will take effect only after the line is over (Example, 15++ will 15 in the same line but when compiler goes to next line the value increases to 16)

`++a => a.inc()`

-- operator decrements the values by 1, the decrement is prefixed that means before reading the value -- operators subtracts 1

`--a => a.dec()`

Complete program for the unary operator

```fun main(args:Array<String>)
{
//example for +
var i = 5
println(+i)
// example for -
var a = 10
println(-a)
// example for !
var flag = false
println(!flag)
// example for ++
var preInc = 12
println(++preInc)
var preDec = 17
println(--preDec)
var postIncre = 3
println(postIncre++)
// example for --
var postDecre = 3
println(postDecre++)
}```

## Equality Operators in Kotlin

What is Equality in Kotlin ?
Equality is nothing but checking the value of the two objects, there are two kinds of equality

• Reference Equality
• Structural Equality

### Reference Equality :

To check whether two objects point to the same location in memory address or not.

### Structural Equality :

To check whether two objects have the same value or not even though they don't point to the same memory location. Equality Operators can also check the negation that non-equality

```fun main(args:Array<String>)
{
var i = 10
var j = i
var k = 10
// reference equality
println(i===j)
// structural equality
println(i==k)
// non equality
println(i!==j)
// non-equality
println(i!=k)
}

```

## in Operator in Kotlin

in operator in Kotlin is used to check whether a particular value exists in a Range or Collection, and the method translates to value1 in value2 => value2.contains(value1)

```fun main(args:Array<String>)
{
var count = 3
val array: IntArray = intArrayOf(1, 2, 3, 4, 5)
if (count in array) {
print("OMG, the number is present in array")
}
}```

You can use the in operator along with negation operator like !in

```fun main(args:Array<String>)
{
var count = 58
val array: IntArray = intArrayOf(1, 2, 3, 4, 5)
if (count !in array) {
print("have you lost senses, the number doesn't exist")
}
}```

in operator can also be used to iterate a collection when we use it with enhanced for loop

```fun main(args:Array<String>)
{
val array: IntArray = intArrayOf(1, 2, 3, 4, 5)
println("Individual values are : ")
for (element in array) {
println(element)

}
}```
```fun main(args:Array<String>)
{
for (item in 1..7){
print("iterates loop from 1 to 7")
}
}
```

## is Operator in Kotlin

is an operator is used to check the type of the variable or object is matching or not. is operator also used to casting but I will discuss it in another chapter.

```fun main(args:Array<String>)
{
var flag=false;
var booleanFlag:Boolean = true
// check whether type is boolean
println(flag is Boolean)
println(booleanFlag is Boolean)
}```

## index Operator in Kotlin

index operator is used to getting a particular value in an array, or to set the values by referencing the particular index.

In kotlin as well the index starts from the 0, so if you have 5 elements in the array, the array will contain index till 4 as it starts from 0.

```fun main(args:Array<String>)
{
val sampleArray: IntArray = intArrayOf(1, 2, 3, 4, 5)
println(sampleArray) // return value 2
}```

We can set the value for the array element by referencing it with index

```fun main(args:Array<String>)
{
val sampleArray: IntArray = intArrayOf(1, 2, 3, 4, 5)
sampleArray = 989 // set the value for the element which is at index 2
println(sampleArray) // return value 989
}
```

## invoke Operator in Kotlin

What is invoke Operator() ?

invoke() is an operator when we override the invoke() operator in a class then we can use the () invoke operator to invoke operator function

invoke method in kotlin is an Operator it calls invoke() function in a class, when a class has operator invoke() is overridden.

You have to use the () invoke operator along with constructor of the class.

```fun main(args:Array<String>)
{
// calls the invoke()
var abc = ABC()()
var xy = XYZ()
// class the invoke()
xy()
}
class ABC{
operator fun invoke(){
println("how dare you to invoke me")
}
fun intel(){
println("yes, you are intelligent")
}
}
class XYZ{
operator fun invoke(){
println("XYZ invoke Operator")
}
}```
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
• chercher
`Please concentrate on spelling mistakes:) all the very best!!!`