Scala Operators
An operator is a symbol that tells the compiler to perform specific 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 variable A is 10, and B is 20:
| Operator | Description | Example |
| --- | --- | --- |
| + | Plus | A + B operation result is 30 |
| - | Minus | A - B operation result is -10 |
| * | Multiply | A * B operation result is 200 |
| / | Divide | B / A operation result is 2 |
| % | Modulus | B % A operation result is 0 |
### Example
## Example
object Test {
def main(args: Array)={
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, 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 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) operation result is false |
| != | Not equal to | (A != B) operation result is true |
| > | Greater than | (A > B) operation result is false |
| < | Less than | (A = | Greater than or equal to | (A >= B) operation result is false |
| <= | Less than or equal to | (A b = " + (a > b));
println("a < b = " + (a = a = " + (b >= a));
println("b <= a = " + (b b = false a = 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) operation result is false |
| || | Logical OR | (A || B) operation result is true |
| ! | Logical NOT | !(A && B) operation result is true |
### Example
## Example
object Test {
def main(args: Array)={
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, the output result is:
$ 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. ~, &, |, ^ are bitwise NOT, bitwise AND, bitwise OR, and bitwise XOR respectively, as shown 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 specified A = 60; and B = 13; the binary representations of the two variables are:
A = 0011 1100 B = 0000 1101-------Bitwise Operations---------- 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) output result 12, binary explanation: 0000 1100 |
| | | Bitwise OR operator | (a | b) output result 61, binary explanation: 0011 1101 |
| ^ | Bitwise XOR operator | (a ^ b) output result 49, binary explanation: 0011 0001 |
| ~ | Bitwise NOT operator | (~a ) output result -61, binary explanation: 1100 0011, in the two's complement form of a signed binary number. |
| << | Left shift operator | a <> | Right shift operator | a >> 2 output result 15, binary explanation: 0000 1111 |
| >>> | Unsigned right shift | A >>>2 output result 15, binary explanation: 0000 1111 |
### Example
## Example
object Test {
def main(args: Array)={
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;/* 15 = 1111 */
println("a >> 2 = " + c );
c = a >>>2;/* 15 = 0000 1111 */
println("a >>> 2 = " + c );
}
}
Executing the above code, the output result is:
$ scalac Test.scala $ scala Test a & b = 12 a | b = 61 a ^ b = 49~a = -61 a <> 2 = 15 a >>> 2 = 15
* * *
## Assignment Operators
The following lists the assignment operators supported by the Scala language:
| Operator | Description | Example |
| --- | --- | --- |
| = | Simple assignment operator, assigns the right operand to the left operand. | C = A + B will assign 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 |
| %= | Modulus and assign, takes the modulus of the left and right operands and then assigns the result to the left operand. | C %= A is equivalent to C = C % A |
| <<= | Left shift and assign | C <<= 2 is equivalent to C = C <>= | 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)={
var a =10;
var b =20;
var c =0;
c = a + b
YouTip