Today we will look into various types of scala operator. An operator tells the compiler to perform specific mathematical or logical operations.

Table of Contents

## Scala Operator

The following types of operators are supported in Scala programming language.

- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators

Let’s go through with each of them one by one with examples.

### Scala Arithmetic Operators

Arithmetic operators include the basic operations like Addition, Subtraction, Multiplication, Division and Modulus. The arithmetic operators supported are +,-,*,/ and %.

Operator | Description |

+ | Adds two operands |

– | Subtracts the second operand from the first |

* | Multiplies two operands |

/ | Divides the numerator by the denominator |

% | Returns the remainder |

Here is an example of scala arithmetic operator.

object Arithmetic { def main(args:Array[String]) { var x = 40; var y = 20; println("Addition of x + y = " + (x + y)); println("Subtraction of x - y = " + (x - y)); println("Multiplication of x * y = " + (x * y)); println("Division of x / y = " + (x / y)); println("Modulus of x % y = " + (x % y)); } }

We are defining an object Arithmetic with the main method which accepts argument of String type of array. x and y variables are declared with values 40 and 20 respectively. We are printing the result by performing operations x+y, x-y, x*y, x/y, x%y.

Note that in Scala unlike in Java we can directly create an object without first creating a class.

Run the above code after creating object Arithmetic by typing `Arithmetic.main(null)`

in Scala shell.

**Output:**

scala> Arithmetic.main(null) Addition of x + y = 60 Subtraction of x - y = 20 Multiplication of x * y = 800 Division of x / y = 2 Modulus of x % y = 0

### Scala Relational Operators

Relational operators include ==,!=,>,<,>= and <=. The following table shows the list of relational operators in scala.

Operator | Description |

== | Checks whether the two operands are equal or not and returns true if they are equal. |

!= | Checks if the two operands are equal or not and returns true if they are not equal. |

> | Checks if the first operand is greater than the second and returns true if the first operand is greater than the second operand. |

< | Checks if the first operand is lesser than the second and returns true if the first operand is lesser than the second operand. |

>= | Checks whether the first operand is greater than or equal to the second operand and returns true if the first operand is greater than or equal to the second operand. |

<= | Checks whether the first operand is lesser than or equal to the second operand and returns true if the first operand is lesser than or equal to the second operand. |

Consider an example for relational operators in scala.

object Relational { def main(args:Array[String]) { var x = 10; var y = 20; println("Equality of x == y is : " + (x == y)); println("Not Equals of x != y is : " + (x !=y)); println("Greater than of x > y is : " + (x > y)); println("Lesser than of x < y is : " + (x < y)); println("Greater than or Equal to of x >= y is : " + (x >= y)); println("Lesser than or Equal to of x <= y is : " + (x <= y)); } }

We are defining an object Relational with the main method which accepts argument of String type of array. x and y variables are declared with values 10 and 20 respectively. We are printing the result by performing operations x ==y, x!=y, x>y, x<y, x>=y and x <=y.

Run the above code by typing Relational.main(null)

**Output:**

scala> Relational.main(null) Equality of x == y is : false Not Equals of x != y is : true Greater than of x > y is : false Lesser than of x < y is : true Greater than or Equal to of x >= y is : false Lesser than or Equal to of x <= y is : true

### Scala Logical Operators

Logical operators include !, || and &&. The following table shows the list of logical operators supported.

Operator | Description |

! | Logical NOT operand which reverses the logical state of the operand.Returns true if the condition is satisfied |

|| | Logical OR operand which returns true if any of the two operands is non zero |

&& | Logical AND operand which returns true if both the operand are non zero |

Consider an example of scala logical operators.

object Logical { def main(args: Array[String]) { var x = false var y = true println("Logical Not of !(x && y) = " + !(x && y) ); println("Logical Or of x || y = " + (x || y) ); println("Logical And of x && y = " + (x &&y) ); } }

We are defining an object Logical with the main method which accepts argument of String type of array. x and y are boolean variables with values false and true respectively. We are printing the result by performing operations !(x && y), x || y and x && y.

Run the above code by typing Logical.main(null)

**Output:**

scala> Logical.main(null) Logical Not of !(x && y) = true Logical Or of x || y = true Logical And of x && y = false

### Bitwise Operators in Scala

Bitwise operators include &, |, ~, ^, <<, >> and >>>. The following table shows the bitwise operators supported in Scala.

Operator | Description |

& | Binary AND operator copies the bit to the result if the operator exists in both the operands. |

| | Binary OR operator copies the bit to the result if the operator exists in either of the operands. |

~ | Binary Ones Complement Operator is unary and has effects of the flipping bits. |

^ | Binary XOR operator copies if the bit is set in one of the operand but not both. |

<< | Binary Left Shift operator.The left operand value is moved left by the number of bits specified in the right operand. |

>> | Binary Right Shift operator.The left operand value is moved right by the number of bits specified by the right operand. |

>>> | Shift right zero fill operator. The left operands value is moved right by the number of bits specified by the right operand and shifted values are filled with zeros. |

Consider an example of bitwise operator in scala.

object Bitwise { def main(args: Array[String]) { var x = 16; var y = 12; var z = 0; z = x & y; println("Bitwise And of x & y = " + z ); z = x | y; println("Bitwise Or of x | y = " + z ); z = x ^ y; println("Bitwise Xor of x ^ y = " + z ); z = ~x; println("Bitwise Ones Complement of ~x = " + z ); z = x << 2; println("Bitwise Left Shift of x << 2 = " + z ); z = x >> 2; println("Bitwise Right Shift of x >> 2 = " + z ); z = x >>> 2; println("Bitwise Shift Right x >>> 2 = " + z ); } }

We are defining an object Bitwise with the main method which accepts argument of String type of array. x and y are variables with values 16 and 12 respectively. We are printing the result by performing all bitwise operations.

Run the program by typing Bitwise.main(null)

**Output:**

scala> Bitwise.main(null) Bitwise And of x & y = 0 Bitwise Or of x | y = 28 Bitwise Xor of x ^ y = 28 Bitwise Ones Complement of ~x = -17 Bitwise Left Shift of x << 2 = 64 Bitwise Right Shift of x >> 2 = 4 Bitwise Shift Right x >>> 2 = 4

### Scala Assignment Operators

Scala Assignment operators include =,+=, -=, *=, /=, %=, <<=, >>=, &=, |= and ^=.The following table shows the list of assignment operators.

Operator | Description |

= | Assigns value from right side operand to left side operand |

+= | Adds right operand to left operand and assigns the result to left operand |

-= | Subtracts right operand from the left operand and assigns the result to left operand |

*= | Multiplies right operand with the left operand and assigns the result to the left operand |

/= | Divides the left operand with the right operand and assigns the result to the left operand |

%= | Finds the modulus of two operands and assigns the result to left operand |

<<= | Left shift assignment operator.It left shifts the operand and assigns result to the left operand |

>>= | Right shift assignment operator.It rights shifts the operator and assigns the value to left operand |

&= | Bitwise AND assignment operator performs bitwise AND operation and assigns the result to left operand |

|= | Bitwise OR assignment operator performs bitwise OR operation and assigns result to left operand |

^= | Performs bitwise exclusive OR operation and assigns result to the left operand |

Consider an example for assignment operators in scala.

object Assignment { def main(args: Array[String]) { var x = 20; var y = 30; var z = 0; z = x + y; println("z= x+ y = " + z ); z+= x ; println("Add and assignment of z += x = " + z ); z -= x ; println("Subtract and assignment of z -= x = " + z ); z *= x ; println("Multiplication and assignment of z *= x = " + z ); x = 20; z = 15; z /= x ; println("Division and assignment of z /= x = " + z ); x = 30; z = 15; z %= x; println("Modulus and assignment of z %= x = " + z ); z <<= 2; println("Left shift and assignment of z <<= 2 = " + z ); z >>= 2; println("Right shift and assignment of z >>= 2 = " + z ); z &= x; println("Bitwise And assignment of z &= 2 = " +z ); z ^= x ; println("Bitwise Xor and assignment of z ^= x = " + z ); z |= x ; println("Bitwise Or and assignment of z |= x = " + z ); } }

We are defining an object Assignment with the main method which accepts argument of String type of array. x and y are variables. We are printing the result by performing all assignment operations.

Run the above example by tying Assignment.main(null)

**Output**:

scala> Assignment.main(null) z= x+ y = 50 Add and assignment of z += x = 70 Subtract and assignment of z -= x = 50 Multiplication and assignment of z *= x = 1000 Division and assignment of z /= x = 0 Modulus and assignment of z %= x = 15 Left shift and assignment of z <<= 2 = 60 Right shift and assignment of z >>= 2 = 15 Bitwise And assignment of z &= 2 = 14 Bitwise Xor and assignment of z ^= x = 16 Bitwise Or and assignment of z |= x = 30

That's all for different types of Scala Operator.

## Leave a Reply