Home »
Scala
Scala Operators
By IncludeHelp Last updated : October 09, 2024
What are operators in Scala?
Scala operators are symbols that represent operations that is to be performed in the program. Operators tell the compiler to perform a specific operation, each operator is associated with one and has a unique identification code. Operators play an important role in programming and they are used to make an expression that executes to perform a task. It defines that operation between operands, based on the operator their can be one, two or three operands.
Types of Scala Operators
Scala as a huge range of operator that can be used while programming in Scala. They are:
- Arithmetic operators
- Relational operators
- Logical operators
- Bitwise operators
- Assignment operators
1) Scala Arithmetic Operators
For performing arithmetic operations in Scala, arithmetic operators are defined. The following arithmetic operators are defined in Scala,
Operator |
Description |
Addition Operator (+) |
Adds two operands 12 + 45 = 57 |
Subtraction Operator (-) |
Subtracts second operator from first 23 - 4 = 19 |
Multiplication Operator (*) |
Multiplies the two operands 25 *6 = 150 |
Division Operator (/) |
Divides the first operator by second 124/4 = 31 |
Modulus Operator (%) |
Gives the remainder when the first operator is divided by the second 123 % 5 = 3 |
Example
object ArithmeticOperatorsExample {
def main(args: Array[String]): Unit = {
val a = 10
val b = 5
// Addition
val sum = a + b
println(s"Addition: $a + $b = $sum")
// Subtraction
val difference = a - b
println(s"Subtraction: $a - $b = $difference")
// Multiplication
val product = a * b
println(s"Multiplication: $a * $b = $product")
// Division
val quotient = a / b
println(s"Division: $a / $b = $quotient")
// Modulus (remainder)
val remainder = a % b
println(s"Modulus: $a % $b = $remainder")
}
}
Output
Addition: 10 + 5 = 15
Subtraction: 10 - 5 = 5
Multiplication: 10 * 5 = 50
Division: 10 / 5 = 2
Modulus: 10 % 5 = 0
2) Scala Relational Operators
A relational operator is used to define the comparison between the two operators. It is also known as a comparison operator. Following are relational Operators in Scala,
Operator |
Description |
Equal to (==) |
Compares if the two operands are equal, if yes then outputs TRUE else FALSE 12 == 34 → FALSE 23 == (46/2) → TRUE |
Not Equal to (!=) |
Compares if the two operands are not equal, if yes then outputs TRUE else FALSE 12 != 34 → TRUE 23 != (46/2) → FALSE |
Greater than (>) |
Checks if operand 1 is greater than operand 2. If yes outputs true otherwise false 34 > 2 → TRUE 34 >334 → FALSE |
Greater than equal to (>=) |
Checks if operand 1 is greater than or equal to operand 2. If yes outputs true otherwise false 34 >= 2 → TRUE 34 >=334 → FALSE 34 >= 34 → TRUE |
Less than (<) |
Checks if operand 1 is smaller than operand 2. If yes outputs true otherwise false 34 < 2 → FALSE 34 < 334 → TRUE |
Less than equal to (<=) |
Checks if operand 1 is smaller than or equal to operand 2. If yes outputs true otherwise false 34 <= 2 → FALSE 34 <= 334 → TRUE 34 <= 34 → TRUE |
Example
object RelationalOperatorsExample {
def main(args: Array[String]): Unit = {
val a = 10
val b = 5
// Equal to
val isEqual = a == b
println(s"$a == $b : $isEqual")
// Not equal to
val isNotEqual = a != b
println(s"$a != $b : $isNotEqual")
// Greater than
val isGreaterThan = a > b
println(s"$a > $b : $isGreaterThan")
// Less than
val isLessThan = a < b
println(s"$a < $b : $isLessThan")
// Greater than or equal to
val isGreaterThanOrEqual = a >= b
println(s"$a >= $b : $isGreaterThanOrEqual")
// Less than or equal to
val isLessThanOrEqual = a <= b
println(s"$a <= $b : $isLessThanOrEqual")
}
}
Output
10 == 5 : false
10 != 5 : true
10 > 5 : true
10 < 5 : false
10 >= 5 : true
10 <= 5 : false
3) Scala Logical Operators
A logical Operator is used to combine the logical output (true / False ) or reverse to the logical output of a condition or operand. Scala defines the following logical operators,
Operator |
Description |
And (&& or and) |
It outputs TRUE, if the logical value of both operands is TRUE, else outputs FALSE. |
OR (|| or or) |
It outputs FALSE, if the logical value of both operands is FALSE, else outputs TRUE. |
NOT (! or not) |
It reverses the output of the operand. |
The results of and , or and, not operator based on different values of operand:
A |
B |
A && B |
A || B |
!A |
True |
True |
True |
True |
False |
True |
False |
False |
True |
False |
False |
True |
False |
True |
True |
False |
False |
False |
False |
True |
Example
object LogicalOperatorsExample {
def main(args: Array[String]): Unit = {
val a = true
val b = false
// Logical AND (&&)
val andResult = a && b
println(s"$a && $b : $andResult")
// Logical OR (||)
val orResult = a || b
println(s"$a || $b : $orResult")
// Logical NOT (!)
val notA = !a
val notB = !b
println(s"!$a : $notA")
println(s"!$b : $notB")
}
}
Output
true && false : false
true || false : true
!true : false
!false : true
4) Scala Bitwise Operators
It works on the bits of the operands. It operates on every bit of the operands to give output. The following are valid bitwise operators in Scala,
Operator |
Description |
Bitwise AND (&) |
It takes every bit of both operands and performs AND operation to it. 2 & 6 = 2 -> 010 & 110 = 010 |
Bitwise OR (|) |
It takes every bit of both operands and performs OR operation to it. 2 | 6 = 6 -> 0010 | 0110 = 0110 |
Bitwise XOR (^) |
It takes every bit of both operands and performs XOR operation to it. XOR operation gives 1 output when both operands are different. 2 ^ 6 = 4 -> 0010 ^ 0110 = 0100 |
Bitwise Left Shift (<<) |
It shifts left the bits of operand 1 by n bits; n is specified by operand 2. 12 << 3 = 96 -> 0000 1100 << 3 = 0110 0000 |
Bitwise Right Shift (>>) |
It shifts right the bits of operand 1 by n bits; n is specified by operand 2. 12 >> 3 = 129 -> 0000 1100 >> 3 = 1000 0001 |
Bitwise Right Shift zero fill (>>>) |
It shifts the bits of operand 1 by n bits; n is specified by operand 2. And fills the shifted bit by 0. 12 >>> 3 = 1 ->0000 1100 >>> 3 000 0001 |
Bitwise Complement (~) |
It takes every bit of the operand and reverses its bits. ~12 = 243 -> ~ (0000 1100) = 1111 0011 |
Example
object BitwiseOperatorsExample {
def main(args: Array[String]): Unit = {
val a = 12 // Binary: 1100
val b = 5 // Binary: 0101
// Bitwise AND (&)
val andResult = a & b // Binary: 0100 (Decimal: 4)
println(s"$a & $b = $andResult")
// Bitwise OR (|)
val orResult = a | b // Binary: 1101 (Decimal: 13)
println(s"$a | $b = $orResult")
// Bitwise XOR (^)
val xorResult = a ^ b // Binary: 1001 (Decimal: 9)
println(s"$a ^ $b = $xorResult")
// Bitwise NOT (~)
val notA = ~a // Binary: 0011... (Inverts all bits, result depends on word size)
println(s"~$a = $notA")
// Left Shift (<<)
val leftShift = a << 2 // Binary: 110000 (Decimal: 48)
println(s"$a << 2 = $leftShift")
// Right Shift (>>)
val rightShift = a >> 2 // Binary: 0011 (Decimal: 3)
println(s"$a >> 2 = $rightShift")
// Unsigned Right Shift (>>>)
val unsignedRightShift = a >>> 2 // Binary: 0011 (Decimal: 3, same as >> in this case)
println(s"$a >>> 2 = $unsignedRightShift")
}
}
Output
12 & 5 = 4
12 | 5 = 13
12 ^ 5 = 9
~12 = -13
12 << 2 = 48
12 >> 2 = 3
12 >>> 2 = 3
5) Scala Assignment Operators
The assignment operator is used to assign value to a variable. The variable is at the left of the operator and value is at the right of the operator. The data type needs to be the same for both sides to avoid errors in the program.
The following are valid assignment operators in Scala,
Operator |
Description |
Simple Assignment (=) |
It simply assigns the value at left to the variable at right. a = 2432; |
Add and assign (+=) |
It adds the left value to the right value. c += 32 -> c = c + 32 |
Subtract and assign (-=) |
It subtracts the left value from the right value. c -= 32 -> c = c - 32 |
Multiply and assign (*=) |
It multiplies the left value with the right value. c *= 32 -> c = c * 32 |
Divide and assign (/=) |
It divides the left value from the right value. c /= 32 -> c = c / 32 |
Remainder and assign (%=) |
It finds the modulus of to the right value divide by left value. c %= 32 -> c = c % 32 |
Left Shift and assign (>>=) |
It shifts the bits of left operands by n , n defined by right operator and assigns this value to left operator. i >>= 6 -> i = i >> 6 |
Right Shift and assign (<<=) |
It shifts the bits of right operands by n , n defined by right operator and assigns this value to left operator. i <<= 6 -> i = i << 6 |
Bitwise and assignment (&=) |
It performs the bitwise and operation of left operand and right operand and assigns this value to left operator. i &= 6 -> i = i & 6 |
Bitwise exclusive OR assignment (^=) |
It performs the bitwise exclusive OR operation of left operand and right operand and assigns this value to left operator. i ^= 6 -> i = i & 6 |
Example
object AssignmentOperatorsExample {
def main(args: Array[String]): Unit = {
var a = 10
var b = 5
// Simple assignment
println(s"Initial value of a: $a")
println(s"Initial value of b: $b")
// Add and assign (+=)
a += b // a = a + b
println(s"After a += b: $a")
// Subtract and assign (-=)
a -= b // a = a - b
println(s"After a -= b: $a")
// Multiply and assign (*=)
a *= b // a = a * b
println(s"After a *= b: $a")
// Divide and assign (/=)
a /= b // a = a / b
println(s"After a /= b: $a")
// Modulus and assign (%=)
a %= b // a = a % b
println(s"After a %= b: $a")
// Bitwise AND and assign (&=)
a = 12 // Binary: 1100
b = 5 // Binary: 0101
a &= b // a = a & b (Binary: 0100, Decimal: 4)
println(s"After a &= b: $a")
// Bitwise OR and assign (|=)
a |= b // a = a | b (Binary: 0101, Decimal: 5)
println(s"After a |= b: $a")
// Bitwise XOR and assign (^=)
a ^= b // a = a ^ b (Binary: 0000, Decimal: 0)
println(s"After a ^= b: $a")
// Left shift and assign (<<=)
a = 3 // Binary: 0011
a <<= 2 // a = a << 2 (Binary: 1100, Decimal: 12)
println(s"After a <<= 2: $a")
// Right shift and assign (>>=)
a >>= 2 // a = a >> 2 (Binary: 0011, Decimal: 3)
println(s"After a >>= 2: $a")
// Unsigned right shift and assign (>>>=)
a >>>= 1 // a = a >>> 1 (Binary: 0001, Decimal: 1)
println(s"After a >>>= 1: $a")
}
}
Output
Initial value of a: 10
Initial value of b: 5
After a += b: 15
After a -= b: 10
After a *= b: 50
After a /= b: 10
After a %= b: 0
After a &= b: 4
After a |= b: 5
After a ^= b: 0
After a <<= 2: 12
After a >>= 2: 3
After a >>>= 1: 1
All the operators are applied in Scala as it is and you can use them easily but to avoid error data type of operands should be the same.