Scala Operators
An operator is a symbol that tells the compiler to perform a specified 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 each 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 sign | A + B gives 30 |
| - | Subtraction | A - B gives -10 |
| * | Multiplication | A * B gives 200 |
| / | Division | B / A gives 2 |
| % | Remainder | B % A gives 0 |
Example
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
)
)
;
}
}
Run Example »
The output of the code above 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 | (A == B) gives false |
| != | Not equal | (A != B) gives true |
| > | Greater than | (A > B) gives false |
| < | Less than | (A < B) gives true |
| >= | Greater than or equal to | (A >= B) gives false |
| <= | Less than or equal to | (A <= B) gives true |
Example
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
)
)
;
}
}
The output of the code above 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) gives false |
| || | Logical OR | (A || B) gives true |
| ! | Logical NOT | !(A && B) gives true |
Example
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
)
)
;
}
}
The output of the code above is:
$ scalac Test.scala
$ scala Test
a && b = false
a || b = true
!(a && b) = true
Bitwise operators
Bitwise operators operate on binary bits; ~, &, |, ^ denote NOT, AND, OR, and XOR operations respectively, as shown in the examples below:
| 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 corresponding binary values 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: 0000 1100 |
| | | Bitwise OR operator | (a | b) outputs 61, binary: 0011 1101 |
| ^ | Bitwise XOR operator | (a ^ b) outputs 49, binary: 0011 0001 |
| ~ | Bitwise NOT operator | (~a ) outputs -61, binary: 1100 0011, in the two's complement form of a signed binary number. |
| << | Left shift operator | a << 2 outputs 240, binary: 1111 0000 |
| >> | Right shift operator | a >> 2 outputs 15, binary: 0000 1111 |
| >>> | Unsigned right shift | A >>>2 outputs 15, binary: 0000 1111 |
Example
Example
object
Test
{
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
)
;
}
}
The output of the code above is:
$ 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 Scala:
| operator | Description | Example |
|---|---|---|
| = | Simple assignment, assigning 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, 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, 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, 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, then assigns the result to the left operand. | C /= A is equivalent to C = C / A |
| %= | Modulo-and-assign: computes the remainder of the left operand divided by the right, then assigns the result 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
object
Test
{
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
)
;
}
}
The output of the code above is:
$ 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 an operator belongs and affects how an expression is evaluated.
Example: x = 7 + 3 * 2; here, x evaluates to 13, not 20, because multiplication () has higher precedence than addition (+), so it computes 32 first, then adds 7.
See the table below; precedence decreases from top to bottom, with the highest at the top and the comma operator having 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 |