English:
Scala Operators
An operator is a symbol that tells the compiler to perform a specific mathematical or logical operation.
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 variable A is 10 and B is 20:
Operator | Description | Example |
---|---|---|
+ | Addition | A + B results in 30 |
- | Subtraction | A - B results in -10 |
* | Multiplication | A * B results in 200 |
/ | Division | B / A results in 2 |
% | Modulus | B % A results in 0 |
Example
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));
}
}
When the above code is executed, the output 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 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) results in false |
!= | Not equal to | (A != B) results in true |
> | Greater than | (A > B) results in false |
< | Less than | (A < B) results in true |
>= | Greater than or equal to | (A >= B) results in false |
<= | Less than or equal to | (A <= B) results in true |
Example
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));
}
}
When the above code is executed, the output is:
$ 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) results in false | ||||
Logical OR | (A | B) results in true | ||||
! | Logical NOT | !(A && B) results in true |
Example
object Test {
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));
}
}
When the above code is executed, the output is:
$ scalac Test.scala
$ scala Test
a && b = false
a || b = true
!(a && b) = true
Bitwise Operators
Bitwise operators are used to manipulate binary bits. The following table shows examples of the bitwise NOT, AND, OR, and XOR operations:
p | q | p & q | p | q | p ^ q |
---|---|---|---|---|---|
0 | 0 | 0 | 0 |
This is a Chinese to English translation, please provide the English translation for this text. Do not provide any explanations or text apart from the translation.
English: c &= a;
println("c &= 2 = " + c);
c ^= a;
println("c ^= a = " + c);
c |= a;
println("c |= a = " + c);
}
}
Execute the above code, and the output will be:
$ 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
---
The order of operator precedence depends on the group of operators it belongs to, which affects the calculation of the expression.
Example: x = 7 + 3 * 2; Here, the result of x is 13, not 20, because multiplication (*) has higher precedence than addition (+), so it calculates 3*2 first and then adds 7.
Check the following table, the 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 |