Scala operators
An operator is a symbol that tells the compiler to perform specified mathematical and logical operations.
Scala has a rich set of built-in operators, including the following types:
Arithmetic operators
Relational operators
Logical operators
Bitwise operators
Assignment operators
Next we will introduce the application of the above operators in detail.
Arithmetic operators
The following table lists the arithmetic operators supported by Scala.
Assume that variable A is 10 and B is 20:
Operator | Description | Example |
---|---|---|
+ | Plus sign | A + B The result of the operation is 30 |
- | Minus sign | A - B The result of the operation is -10 |
Multiply sign | A * B The result of the operation is 200 | |
Division sign | B / A The result of the operation is 2 | |
Remainder | B % A The operation result is 0 |
object Test {
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) );
}
}
Execute the above code, the output result is: $ scalac Test.scala $ scala Test a + b = 30 a - b = -10 a * b = 200 b / a = 2 b % a = 0 c % a = 5
Relational operatorsThe following table lists the relational operators supported by Scala. Assume that variable A is 10 and B is 20:
Description | Example | |
---|---|---|
is equal to | (A == B) The operation result is false | |
is not equal to | (A != B) The operation result is true | |
is greater than | ( A > B) The operation result is false | |
is less than | (A < B) The operation result is true | |
Greater than or equal to | (A >= B) The operation result is false | |
Less than or equal to | (A <= B) The operation result is true |
object Test { 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) ); } }
Execution The above code, the output result is:
$ scalac Test.scala $ scala Test a == b = false a != b = true a > b = false a < b = true b >= a = true b <= a = falseLogical operators
The following table lists the logical operators supported by Scala.
Assume that variable A is 1 and B is 0:
Description | Example | |||
---|---|---|---|---|
Logical AND | (A && B) The result of the operation is false | |||
Logical OR | (A || B) The result of the operation is true | |||
Logical NOT | !( A && B) The operation result is true |
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 you specify A = 60; and B = 13;, the binary corresponding to the two variables is:
A = 0011 1100 B = 0000 1101 -------位运算---------- A&B = 0000 1100 A|B = 0011 1101 A^B = 0011 0001 ~A = 1100 0011
The bitwise arithmetic rules in Scala are as follows:
Operator | Description | Example |
---|---|---|
Bitwise AND operation Symbol | (a & b) The output result is 12, binary interpretation: 0000 1100 | |
Bitwise OR operator | (a | b) The output result is 61, binary interpretation: 0011 1101 | |
Bitwise XOR operator | (a ^ b ) Output result 49, binary interpretation: 0011 0001 | |
Bitwise negation operator | (~a ) Output result -61, Binary interpretation: 1100 0011, in the two's complement form of a signed binary number. | |
Left shift operator | a << 2 The output result is 240, binary interpretation: 1111 0000 | |
Right shift operator | a >> 2 The output result is 15, binary interpretation: 0000 1111 | |
Unsigned right shift | A >>>2 The output result is 15, binary interpretation: 0000 1111 |
Operator | Description | Instance |
---|---|---|
= | A simple assignment operation, assigning the right operand to the left operand. | C = A + B Assign the operation result of A + B to C |
+= | , add it and then assign it, add the left and right The operands are added and then assigned to the operand on the left. | C += A is equivalent to C = C + A |
-= | subtract and then assign, subtract the left and right operands Then assign the value to the operand on the left. | C -= A is equivalent to C = C - A |
*= | . Multiply and then assign. Multiply the left and right operands. Then assign the value to the operand on the left. | C *= A is equivalent to C = C * A |
/= | and then assign the value. Divide the left and right operands. Then assign the value to the operand on the left. | C /= A is equivalent to C = C / A |
Find the remainder and then assign the value. Find the remainder of the left and right operands. Then assign the value to the operand on the left. | C %= A is equivalent to C = C % A | |
Shift left bitwise and then assign value | C <<= 2 is equivalent to C = C << 2 | |
Shift right bitwise and then assign the value | C >>= 2 is equivalent to C = C >> 2 | |
Assignment after bitwise AND operation | C &= 2 is equivalent to C = C & 2 | |
bitwise XOR operator and then assignment | C ^= 2 is equivalent to C = C ^ 2 | |
bitwise OR operation and then assignment | C |= 2 is equivalent In C = C | 2 |
Operator type | Operator | Combining direction |
---|---|---|
Expression Operation | () [] . expr++ expr-- | Left to right |
Unary operator | * & + - ! ~ ++expr --expr * / % + - >> << < > <= >= == != | right to left |
bit operator | & ^ | && || | Left to right |
Ternary operator | ?: | Right to left |
Assignment operator | = += -= *= /= %= >>= <<= &= ^= |= | right to left |
comma | , | left to right |