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

Scala Operator

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

  1. Arithmetic Operators
  2. Relational Operators
  3. Logical Operators
  4. Bitwise Operators
  5. 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.

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 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.

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 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.

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:

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.

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 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.

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-Arithmetic-Operator
That’s all for different types of Scala Operator.

By admin

Leave a Reply