In this lesson, you will learn about the operators in Scala. An operator is a symbol that is used to perform certain operations on values or variables called operands.
We would cover the following 5 types of operators in Scala
1. Arithmetic Operators
Scala supports the same set of arithmetic operators as in Java. The table below provides a list of the arithmetic operators supported in Scala.
Assuming x = 10 and y = 20
| + | Addition – Sum the two operands | x + y will give 30 |
| – | Subtraction – Subtracts second operand y from the first operand x | x – y will give -10 |
| * | Multiplication – Multiplies the two operands | x * y will give 200 |
| / | Division – Divides numerator by de-numerator | y / x will give 2 |
| % | Modulus operator – gets the remainder after division of one number by another |
2. Logical Operators
Logical operators allows you to perform logical operation on given operands. The table below provides a list of logical operators supported by Scala.
Assuming x = 1 and y = 0
| Operator | Description | Example |
|---|---|---|
| && | The Logical AND operator – If both the operands are non zero then condition becomes true. | (x && y) is false. |
| || | The Logical OR Operator – If any of the two operands is non zero then condition becomes true. | (x || y) is true. |
| ! | The Logical NOT Operator -Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. | !(x && y) is true. |
3. Relational Operators
Relational operators are sometimes called comparison operator and they are used to compare two different values.
Assuming x = 10 and y = 20, the the table below summarises the relational operations on the two operands.
| Operator | Description | Example |
|---|---|---|
| == | Checks whether the values of two operands are equal or not, if yes then condition becomes true. | (x == y) is not true. |
| != | Checks whether the values of two operands are equal or not, if values are not equal then condition becomes true. | (x != y) is true. |
| > | Checks whether the value of left operand is greater than the value of right operand, if yes then condition becomes true. | (x > y) is not true. |
| < | Checks whether the value of left operand is less than the value of right operand, if yes then condition becomes true. | (x < y) is true. |
| >= | Checks whether the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. | (x >= y) is not true. |
| <= | Checks whether the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. | (x <= y) is true. |
4. Bitwise Operators
Bitwise operators performs operation on individual bits of the operand. The three common bitwise operators are
- bitwise AND (&&) – return 1 of both operands are 1
- bitwise OR(||) – returns 1 of any of the operand is 1
- XOR (ˆ) – returns 1 of both operands are different
The table below summarise the bitwise operators in Scala assuming
- x = 0011 1100 (60)
- y = 0000 1101 (13)
| Operator | Description | Example |
|---|---|---|
| & | Bitwise AND Operator copies a bit to the result if it exists in both operands. | (x & y) will give 12, which is 0000 1100 |
| | | Bitwise OR Operator copies a bit if it exists in either operand. | (x | y) will give 61, which is 0011 1101 |
| ^ | Bitwise XOR Operator copies the bit if it is set in one operand but not both. | (x ^ y) will give 49, which is 0011 0001 |
| ~ | Bitwise Ones Complement Operator is unary and has the effect of ‘flipping’ bits. | (~x ) will give -61, which is 1100 0011 in 2’s complement form due to a signed binary number. |
| << | Bitwise Left Shift Operator. The bit positions of the left operands value is shifted left by the number of bits specified by the right operand. | x << 2 will give 240, which is 1111 0000 |
| >> | Bitwise Right Shift Operator. The Bit positions of the left operand value is shifted right by the number of bits specified by the right operand. | x >> 2 will give 15, which is 1111 |
| >>> | Bitwise Shift right zero fill operator. The left operands value is shifted right by the number of bits specified by the right operand and shifted values are filled up with zeros. | >>>2 will give 15 which is 0000 1111 |
5. Assignment Operators
The table below provides a list of assignment operators supported by Scala.
| Operator | Description | Example |
|---|---|---|
| = | Simple assignment operator – this assigns values from right side operands to left side operand | z = x + y will assign value of x + y into z |
| += | Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand | z += x is equivalent to z = z + x |
| -= | Subtraction AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand | z -= x is equivalent to z = z – x |
| *= | Multiplication AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand | z *= x is equivalent to z = z * x |
| /= | Division AND assignment operator, It divides left operand with the right operand and assign the result to left operand | z /= A is equivalent to z = z / x |
| %= | Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand | z %= A is equivalent to z = z % x |
| <<= | Left shift AND assignment operator | z <<= 2 is same as z = z << 2 |
| >>= | Right shift AND assignment operator | z >>= 2 is same as z = z >> 2 |
| &= | Bitwise AND assignment operator | z &= 2 is same as z = z & 2 |
| ^= | bitwise exclusive OR and assignment operator | z ^= 2 is same as z = z ^ 2 |
| |= | bitwise inclusive OR and assignment operator | z |= 2 is same as z = z | 2 |