Scala Operators
An operator is a symbol that tells the compiler to perform specified mathematical and logical operations.
Scala is rich in built-in operators, including the following types:
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Next, we will introduce the applications of the above operators in detail.
Arithmetic Operators
The following table lists the arithmetic operators supported by Scala.
Assume variable A is 10 and B is 20:
| Operator | Description | Example |
|---|---|---|
| + | Plus | A + B evaluates to 30 |
| - | Minus | A - B evaluates to -10 |
| * | Multiply | A * B evaluates to 200 |
| / | Divide | B / A evaluates to 2 |
| % | Modulo | B % A evaluates to 0 |
Example
Example
def main(args: Array[String]) = {
var a = 10;
var b = 20;
var c = 25;
var d = 25;
println("a + b = " + (a + b) );
println("a - b = " + (a - b) );
println("a * b = " + (a * b) );
println("b / a = " + (b / a) );
println("b % a = " + (b % a) );
println("c % a = " + (c % a) );
}
}
Run example »
Executing the above code produces the following output:
$ scalac Test.scala $ scala Test a + b = 30 a - b = -10 a * b = 200 b / a = 2 b % a = 0 c % a = 5
Relational Operators
The following table lists the relational operators supported by Scala.
Assume variable A is 10 and B is 20:
| Operator | Description | Example |
|---|---|---|
| == | Equal to | (A == B) evaluates to false |
| != | Not equal to | (A != B) evaluates to true |
| > | Greater than | (A > B) evaluates to false |
| < | Less than | (A < B) evaluates to true |
| >= | Greater than or equal to | (A >= B) evaluates to false |
| <= | Less than or equal to | (A <= B) evaluates to true |
Example
Example
def main(args: Array[String]) = {
var a = 10;
var b = 20;
println("a == b = " + (a == b) );
println("a != b = " + (a != b) );
println("a > b = " + (a > b) );
println("a < b = " + (a < b) );
println("b >= a = " + (b >= a) );
println("b <= a = " + (b <= a) );
}
}
Executing the above code produces the following output:
$ scalac Test.scala $ scala Test a == b = false a != b = true a > b = false a < b = true b >= a = true b <= a = false
Logical Operators
The following table lists the logical operators supported by Scala.
Assume variable A is 1 and B is 0:
| Operator | Description | Example |
|---|---|---|
| && | Logical AND | (A && B) evaluates to false |
| || | Logical OR | (A || B) evaluates to true |
| ! | Logical NOT | !(A && B) evaluates to true |
Example
Example
def main(args: Array[String]) = {
var a = true;
var b = false;
println("a && b = " + (a&&b) );
println("a || b = " + (a||b) );
println("!(a && b) = " + !(a && b) );
}
}
Executing the above code produces the following output:
$ scalac Test.scala $ scala Test a && b = false a || b = true !(a && b) = true
Bitwise Operators
Bitwise operators are used to operate on binary bits,~,&,|,^namely bitwise NOT, bitwise AND, bitwise OR, and bitwise XOR operations, as shown in the examples in the following table:
| p | q | p & q | p | q | p ^ q |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 1 | 1 | 1 | 0 |
| 1 | 0 | 0 | 1 | 1 |
If A = 60; and B = 13; the binary representations of the two variables are:
A = 0011 1100 B = 0000 1101 -------位运算---------- A&B = 0000 1100 A|B = 0011 1101 A^B = 0011 0001 ~A = 1100 0011
The bitwise operation rules in Scala are as follows:
| Operator | Description | Example |
|---|---|---|
| & | Bitwise AND operator | (a & b) outputs 12, binary explanation: 0000 1100 |
| | | Bitwise OR operator | (a | b) outputs 61, binary explanation: 0011 1101 |
| ^ | Bitwise XOR operator | (a ^ b) outputs 49, binary explanation: 0011 0001 |
| ~ | Bitwise NOT operator | (~a) outputs -61, binary explanation: 1100 0011, in the two's complement form of a signed binary number. |
| << | Left shift operator | a << 2 outputs 240, binary explanation: 1111 0000 |
| >> | Right shift operator | a >> 2 outputs 15, binary explanation: 0000 1111 |
| >>> | Unsigned right shift | A >>> 2 outputs 15, binary explanation: 0000 1111 |
Example
Example
def main(args: Array[String]) = {
var a = 60; /* 60 = 0011 1100 */
var b = 13; /* 13 = 0000 1101 */
var c = 0;
c = a & b; /* 12 = 0000 1100 */
println("a & b = " + c );
c = a | b; /* 61 = 0011 1101 */
println("a | b = " + c );
c = a ^ b; /* 49 = 0011 0001 */
println("a ^ b = " + c );
c = ~a; /* -61 = 1100 0011 */
println("~a = " + c );
c = a << 2; /* 240 = 1111 0000 */
println("a << 2 = " + c );
c = a >> 2; /* 15 = 1111 */
println("a >> 2 = " + c );
c = a >>> 2; /* 15 = 0000 1111 */
println("a >>> 2 = " + c );
}
}
Executing the above code produces the following output:
$ scalac Test.scala $ scala Test a & b = 12 a | b = 61 a ^ b = 49 ~a = -61 a << 2 = 240 a >> 2 = 15 a >>> 2 = 15
Assignment Operators
The following lists the assignment operators supported by the Scala language:
| Operator | Description | Example |
|---|---|---|
| = | Simple assignment, assigns the value of the right operand to the left operand. | C = A + B assigns the result of A + B to C |
| += | Add and assign, adds the left and right operands and then assigns the result to the left operand. | C += A is equivalent to C = C + A |
| -= | Subtract and assign, subtracts the right operand from the left operand and then assigns the result to the left operand. | C -= A is equivalent to C = C - A |
| *= | Multiply and assign, multiplies the left and right operands and then assigns the result to the left operand. | C *= A is equivalent to C = C * A |
| /= | Divide and assign, divides the left operand by the right operand and then assigns the result to the left operand. | C /= A is equivalent to C = C / A |
| %= | Modulo and assign, takes the remainder of dividing the left operand by the right operand and assigns it to the left operand. | C %= A is equivalent to C = C % A |
| <<= | Bitwise left shift and assign | C <<= 2 is equivalent to C = C << 2 |
| >>= | Bitwise right shift and assign | C >>= 2 is equivalent to C = C >> 2 |
| &= | Bitwise AND and assign | C &= 2 is equivalent to C = C & 2 |
| ^= | Bitwise XOR and assign | C ^= 2 is equivalent to C = C ^ 2 |
| |= | Bitwise OR and assign | C |= 2 is equivalent to C = C | 2 |
Example
Example
def main(args: Array[String]) = {
var a = 10;
var b = 20;
var c = 0;
c = a + b;
println("c = a + b = " + c );
c += a ;
println("c += a = " + c );
c -= a ;
println("c -= a = " + c );
c *= a ;
println("c *= a = " + c );
a = 10;
c = 15;
c /= a ;
println("c /= a = " + c );
a = 10;
c = 15;
c %= a ;
println("c %= a = " + c );
c <<= 2 ;
println("c <<= 2 = " + c );
c >>= 2 ;
println("c >>= 2 = " + c );
c >>= a ;
println("c >>= a = " + c );
c &= a ;
println("c &= 2 = " + c );
c ^= a ;
println("c ^= a = " + c );
c |= a ;
println("c |= a = " + c );
}
}
Executing the above code produces the following output:
$ scalac Test.scala $ scala Test c = a + b = 30 c += a = 40 c -= a = 30 c *= a = 300 c /= a = 1 c %= a = 5 c <<= 2 = 20 c >>= 2 = 5 c >>= a = 0 c &= 2 = 0 c ^= a = 10 c |= a = 10
Operator precedence depends on the operator group to which the operator belongs, and it affects the evaluation of an expression.
Example: x = 7 + 3 * 2; here, x evaluates to 13, not 20, because multiplication (*) has higher precedence than addition (+), so it first calculates 3*2 and then adds 7.
Refer to the following table. Precedence decreases from top to bottom; the top has the highest precedence, and the comma operator has the lowest precedence.
| Category | Operator | Associativity |
|---|---|---|
| 1 | () [] | Left to right |
| 2 | ! ~ | Right to left |
| 3 | * / % | Left to right |
| 4 | + - | Left to right |
| 5 | >> >>> << | Left to right |
| 6 | > >= < <= | Left to right |
| 7 | == != | Left to right |
| 8 | & | Left to right |
| 9 | ^ | Left to right |
| 10 | | | Left to right |
| 11 | && | Left to right |
| 12 | || | Left to right |
| 13 | = += -= *= /= %= >>= <<= &= ^= |= | Right to left |
| 14 | , | Left to right |