Introduction to Scala Operators
Operators are used to perform logical and mathematical computation in any programming language. Scala also has various operators to perform various calculations and tasks but they are implemented as methods as Scala is an objectoriented language hence it treats every symbol as object and operation as a method. They make computation simple and easy.
Different operators present in Scala are:
 Arithmetic operators
 Assignment operators
 Relational operators
 Logical operators
 Bitwise operators
Now let us study each operator in detail.
Scala Arithmetic Operators
These operators are used to perform mathematical calculations or computations.
Operator  Symbol  Explanation  Format 
Addition  +  Adds both operands  x + y 
Subtraction  –  Subtracts right operand from the left one  x – y 
Multiplication  *  Multiplies both the operands  x * y 
Division  /  Divide numerator by the denominator  x / y 
Modulus  %  Returns remainder after division  x % y 
Example: Arithmetic Operators in Scala
Code:
object Arith {
def main (args: Array [String]) {
var a = 10;
var b = 5;
println (a + b);
println (a – b);
println (a * b);
println (a / b);
println (a % b)
}
}
Output:
scala> Arith.main (null)
15
5
50
2
0
Scala Assignment Operators
These operators are used to assign values to a variable or an object.
Operator  Symbol  Explanation  Format 
Assignment  =  Assigns the value of right operand to the left operand  x = y + z 
Addition  +=  Adds both operands and finally assign the value to the left operand  x += y 
Subtraction  =  Subtracts right operand from the left and then assign the value to the left operand  x = y 
Multiplication  *=  Multiplies both operands and assign the value to the left operand  x *= y 
Division  /=  Divides left operand by the right operand and assign the value to the left operand  x /= y 
Modulus  %=  Evaluates modulus of two operands and assign the value to the left operand  x %= y

Bitwise AND  &=  Compares the binary value of two operands, return 1 if both operands are 1 else return 0 and assign the value to the left operand  x &= 5 
Bitwise OR  =  Compares the binary value of two operands, return 0 if both operands are 0 else return 1 and assign the value to the left operand  x = 5 
Bitwise XOR  ^=  Compares the binary value of two operands, return 0 if both operands are same else return 1 and assign the value to the left operand  x ^= 5 
Left shift  <<=  Shifts the bits towards left and assign the result to the left operand  x <<= 5

Right shift  >>=  Shifts the bits towards the right and assign the result to the left operand  x >>= 5 
Example: Assignment operators in Scala
Code:
object Assign {
def main (args: Array [String]) {
var a = 10;
var b = 5;
println (a += b);
println (a –= b);
println (a *= b);
println (a /= b);
println (a %= b);
a = 20;
b = 15;
println (a &= b);
println (a = b);
println (a ^= b);
println (a <<= 2);
println (a >>= 2);
}
}
4.5 (3,430 ratings)
View Course
Output:
scala> Assign.main (null)
15
10
50
10
0
4
11
4
16
4
Scala Relational Operators
These operators return Boolean value after checking the mentioned conditions.
Operator  Symbol  Explanation  Format 
Equal to  ==  Returns true if both operands are equal else return false  x == y 
Not Equal to  !=  Returns true if both operands are not equal else return false  x != y 
Greater than  >  Returns true if the left operand is greater than right else return false  x > y 
Less than  <  Returns true if the left operand is smaller than right else return false  x < y 
Greater than or equal to  >=  Returns true if the left operand is greater than or equal to the right else return false  x >= y 
Less than or equal to  <=  Returns true if the left operand is smaller than or equal to the right else return false  x <= y 
Example: Relational Operators in scala
Code:
object Relation {
def main (args: Array [String]) {
var a = 10;
var b = 5;
println (a == b);
println (a != b);
println (a > b);
println (a < b);
println (a >= b);
println (a <= b);
}
}
Output:
scala> Relation.main (null)
false
true
true
false
true
false
Scala Logical Operator
These operators also return Boolean value according to the inputs or operands.
Operator  Symbol  Explanation  Format 
Logical AND  &&  Returns true if both operands are non zero else return false  x && y

Logical OR    Returns true if any of the operands is nonzero else return false  x  y

Logical NOT  !  It reverses the operand. Returns true for false and vice versa  !x 
Example: Logical operators in Scala
Code:
object Logic {
def main (args: Array [String]) {
var a = true;
var b = false;
println (a && b);
println (a  b);
println !(b);
}
}
Output:
scala> Logic.main (null)
false
true
true
Scala Bitwise Operators
These operators work on bits and return corresponding integer value as output.
Operator  Symbol  Explanation  Format 
Binary AND  &  Check the operands bitwise and return 1 if both bits are 1 else return 0  x & y 
Binary OR    Check the operands bitwise and return 0 if both bits are 0 else return 1  x  y 
Binary XOR  ^  Check the operands bitwise and return 0 if both bits are same else return 1  x ^ y 
Binary NOT  ~  Returns ones complement i.e. changes 1 to 0 and vice versa  ~x

Binary Left Shift  <<  Bits of the left operand is shifted left side by the number of bits mentioned by the right operand  x << 3 
Binary Right Shift  >>  Bits of the left operand is shifted right side by the number of bits mentioned by the right operand  x >> 3 
Binary Right Shift zero fill  >>>  Bits of the left operand is shifted right side by the number of bits mentioned by right operand and shifted values are substituted bu zeroes.  x >>> 3 
Example: Bitwise Operators in Scala
Code:
object Bit {
def main (args: Array [String]) {
var a = 10;
var b = 5;
println (a & b);
println (a  b);
println (a ^ b);
println ( ~ b);
a = 16;
b = 12;
println (a >> b);
println (a << b);
println (a >>> b);
}
}
Output:
scala> Bit. main (null)
0
15
15
10
4
64
4
Recommended Articles
This has been a guide to Scala Operators. Here we discuss the basic concept and different types of Scala Operators with examples. You may also look at the following articles to learn more –