What is Scala Operator – 6 Major Types of Operators in Scala
Scala course with real-time projects Start Now!!
In this Scala operator tutorial, we will disucss what is an operator in Scala. Moreover, we will discuss various types of Scala operators:Â Arithmetic, Relational, Logical, Bitwise, Assignment Operators in Scala with their syntax and examples.
So, let’s start Scala Operator Tutorial.
What are the Operators in Scala?
A symbol that instructs the compiler to perform certain mathematical or logical manipulations, is an operator.
Types of Scala Operators
We have the following types of Scala Operator:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
Let’s discuss all Scala operator one by one.
a. Arithmetic Operators in Scala
This Scala operator tells Scala to perform arithmetic operations on two values. We have five of these. Let’s take two values for exemplary purposes. (We did this in the Command Prompt).
scala> val a=2 a: Int = 2 scala> val b=3 b: Int = 3
i. Addition (+)
This adds two operands. See an example.
scala> a+b res0: Int = 5
ii. Subtraction (-)
This subtracts the second operand from the first.
scala> a-b res1: Int = -1
iii. Multiplication (*)
This multiplies the two operands.
scala> a*b res2: Int = 6
iv. Division (/)
This divides the first value by the second.
scala> a/b res3: Int = 0 scala> b/a res4: Int = 1
v. Modulus (%)
This returns the remainder after dividing the first number by the second.
scala> a%b res6: Int = 2 scala> b%a res5: Int = 1
b. Relational Operators in Scala
Now, let’s discuss the Scala operator that let us compare values. They are rational Scala Operator.
i. Equal to (==)
This returns true if the two values are equal; otherwise, false.
scala> a==b res7: Boolean = false scala> println(a==b) false
ii. Not Equal to (!=)
This returns true if the two values are unequal; otherwise, false.
scala> a!=b res9: Boolean = true
iii. Greater Than (>)
This returns true if the first operand is greater than the second; otherwise, false.
scala> a>b res10: Boolean = false
iv. Less Than (<)
This returns true if the first operand is lesser than the second; otherwise, false.
scala> a<b res11: Boolean = true
v. Greater Than or Equal to (>=)
This returns true if the first operand is greater than or equal to the second; otherwise, false.
scala> a>=b res12: Boolean = false
vi. Less Than or Equal to (<=)
This returns true if the first operand is less than or equal to the second; otherwise, false.
scala> a<=b res13: Boolean = true
c. Logical Operators in Scala
Moving on to logical Scala operator, we have three. Let’s take Boolean values for this.
i. Logical AND (&&)
This returns true if both operands are true; otherwise, false.
scala> true&&true res22: Boolean = true scala> true&&false res23: Boolean = false
ii. Logical OR (||)
This returns true if either or both operands are true; otherwise, false.
scala> true||false res25: Boolean = true scala> false||false res26: Boolean = false
iii. Logical NOT (!)
This reverses the operand’s logical state. It turns true to false, and false to true.
scala> !true res27: Boolean = false scala> !false res28: Boolean = true scala> !(true&&false) res29: Boolean = true
d. Bitwise Operators in Scala
Coming to Bitwise Scala operator, we have seven in Scala. But first, let’s see the truth table for these:
p | q | p&q | p|q | p^q |
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 0 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
Let’s take two values:
scala> var a=60 //Its binary is 00111100 a: Int = 60 scala> var b=13 //Its binary is 00001101 b: Int = 13
i. Binary AND Operator (&)
This performs an AND operation on each pair of bits in the binary equivalents for the two operands.
scala> a&b res43: Int = 12
This gives us 12 because of 00001100.
ii. Binary OR Operator (|)
This performs an OR operation on each pair of bits in the binary equivalents for the two operands.
scala> a|b res44: Int = 61
The result is 61 because of 00111101.
iii. Binary XOR Operator (^)
This Scala Operator performs an XOR operation on each pair of bits in the binary equivalents for the two operands.
scala> a^b res45: Int = 49
49 is decimal for 00110001.
iv. Binary One’s Complement (~)
This flips the bits in the operand’s binary equivalent.
scala> ~a //This will be 11000011 res46: Int = -61 scala> ~b res47: Int = -14
v. Binary Left-Shift Operator (<<)
This Scala operator moves the bits of the left operand by (the right operand) number of bits to the left.
scala> a<<2 res48: Int = 240
This is because it turns 00111100 to 11110000.
vi. Binary Right-Shift Operator (>>)
This Scala Operator moves the bits of the left operand by (the right operand) number of bits to the right.
scala> a>>2 res49: Int = 15
This is because it turns 00111100 to 1111.
vii. Shift Right Zero Fill Operator (>>>)
Shift Right Zero Fill Scala Operator moves the bits of the left operand by (the right operand) number of bits to the right, and fills these places with zeroes.
scala> a>>>2 res50: Int = 15
This turns 00111100 to 00001111.
e. Assignment Operators in Scala
Scala supports 11 different assignment operators:
i. Simple Assignment Operator (=)
This assigns the value on the right to the operand on the left.
scala> var a=1+2 a: Int = 3
ii. Add and Assignment Operator (+=)
This Scala Operator adds the value of the right operand to the one on the left, and then assigns the result to the left operand.
scala> a+=2 //This is the same as a=a+2 scala> a res31: Int = 5
iii. Subtract and Assignment Operator (-=)
This Scala operator subtracts the second operand from the first, then assigns the result to the left operand.
scala> a-=3 scala> a res33: Int = 2
iv. Multiply and Assignment Operator (*=)
This Scala Operator multiplies the two operands, and then assigns the result to the left operand.
scala>Â a*=7 scala> a res38: Int = 14
v. Divide and Assignment Operator (/=)
Divide And Assignment Scala Operator divides the first operand by the second, and then assigns the result to the left operand.
scala> a/=2 scala> a res40: Int = 7
vi. Modulus and Assignment Operator (%=)
Modulus And Assignment Scala Operator calculates the modulus remaining after dividing the first operand by the second. Then, it assigns this result to the first operand.
scala> a%=4 scala> a res42: Int = 3
vii. Left-Shift and Assignment Operator (<<=)
This Scala operator shifts the left operand by (the right operand) number of bits to the left. Then, it assigns the result to the left operand.
scala> a<<=2 scala> a res57: Int = 240
We’ll reset a to 60 every time so we can properly demonstrate the operators.
scala> a=60 a: Int = 60
viii. Right-Shift and Assignment Operator (>>=)
This Scala Operator shifts the left operand by (the right operand) number of bits to the right. Then, it assigns the result to the left operand.
scala> a>>=2 scala> a res59: Int = 15 scala> a=60 a: Int = 60
ix. Bitwise AND Assignment Operator (&=)
This applies the bitwise AND operator on the two operands, and then assigns the result to the left operand.
scala> a&=b scala> a res61: Int = 12 scala> a=60 a: Int = 60
x. Bitwise Exclusive-OR and Assignment Operator (^=)
This performs a bitwise XOR operation on the two operands, and then assigns this result to the left operand.
scala> a^=b scala> a res63: Int = 49 scala> a=60 a: Int = 60
xi. Bitwise Inclusive-OR and Assignment Operator (|=)
This performs a bitwise-inclusive-OR operation on the two operands, and then assigns this result to the left operand.
scala> a|=b scala> a res65: Int = 61
Scala Operator Precedence
When we use multiple terms in an expression, how does Scala group them? This is decided by Scala operator precedence; this is how an expression evaluates. Some Scala operators have a higher precedence than others:
Category | Operator | Associativity |
Postfix | () [] | Left to Right |
Unary | ! ~ | Right to Left |
Multiplicative | * / % | Left to Right |
Additive | + – | Left to Right |
Shift | >> >>> << | Left to Right |
Relational | > >= < <= | Left to Right |
Equality | == != | Left to Right |
Bitwise AND | & | Left to Right |
Bitwise XOR | ^ | Left to Right |
Bitwise OR | | | Left to Right |
Logical AND | && | Left to Right |
Logical OR | || | Left to Right |
Assignment | = += -= *= /= %= >>= <<= &= ^= |= | Right to Left |
Comma | , | Left to Right |
In the table, the precedence decreases from top to bottom. Hence, the topmost operators have the highest precedencies. In an expression, the operators with a higher precedence evaluate first.
For example, in val x=7+7*7, what evaluates first is 7*7, because * has a higher precedence than +. Then, 7+49 gives us 56.
So, this was all about Scala Operator. Hope you like our explanation.
Conclusion
Hence, we discussed types of Scala operators: Arithmetic, Relational, Logical, Bitwise, Assignment Operators in Scala with their subtypes and examples. Tell us what you think in the comments section.
If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google