English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Scala Operators

An operator is a symbol used to tell the compiler to perform the specified mathematical and logical operations.

Scala contains a wealth 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 various operators in detail.

Arithmetic operators

The following table lists the arithmetic operators supported by Scala.

Assuming the variable A is 10, B is 20:

OperatorsDescriptionExample
+Addition signA + B operation result is 30
-Subtraction signA - B operation result is -10
*Multiplication signA * B operation result is 200
/Division signB / A operation result is 2
%ModulusB % A operation result is 0

Online 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 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.

Assuming the variable A is 10, B is 20:

OperatorsDescriptionExample
==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 < B) operation result is true
>=Greater than or equal to(A >= B) operation result is false
<=Less than or equal to(A <= B) operation result is true

Online 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) );
   }
}

When the above code is executed, 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 = false

Logical operators

The following table lists the logical operators supported by Scala.

Assuming the variable A is 1, B is 0:

OperatorsDescriptionExample
&&Logical AND(A && B) operation result is false
||Logical OR(A || B) operation result is true
!Logical NOT!(A && B) operation result is true

Online 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 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, ~, & , |, ^ respectively represent bitwise NOT, bitwise AND, bitwise OR, and bitwise XOR operations, as shown in the following examples:

pqp & qp | qp ^ q
00000
01011
11110
10011

If A is specified = 60; and B = 13; The binary representation of the two variables is as follows:

A = 0011 1100
B = 0000 1101
-------Bitwise operation----------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A  = 1100 0011

The bitwise operation rules in Scala are as follows:

OperatorsDescriptionExample
&Bitwise AND operator(a & b) Output result 12 ,Binary interpretation: 0000 1100
|Bitwise OR operator(a | b) Output result 61 , Binary interpretation: 0011 1101
^Bitwise XOR operatorOutput result of (a ^ b) 49 , Binary interpretation: 0011 0001
~Bitwise NOT operatorOutput result of (~a ) -61 , Binary interpretation: 1100 0011, in the complement form of a signed binary number.
<<Left shift operatora << 2 Output result 240, Binary interpretation: 1111 0000
>>Right shift operatora >> 2 Output result 15 ,Binary interpretation: 0000 1111
>>>Unsigned right shiftA >>>2 Output result 15, Binary interpretation: 0000 1111

Online 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 );
   }
}

When the above code is executed, the output result 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 the Scala language:

OperatorsDescriptionExample
=Simple assignment operation, specifies the right operand to be assigned to the left operand.C = A + B assigns A + The operation result of B is assigned to C
+=Assignment after addition, the operands on both sides are added and then assigned to the left operand.C += A is equivalent to C = C + A
-=Assignment after subtraction, the operands on both sides are subtracted and then assigned to the left operand.C -= A is equivalent to C = C - A
*=Assignment after multiplication, the operands on both sides are multiplied and then assigned to the left operand.C *= A is equivalent to C = C * A
/=Assignment after division, the operands on both sides are divided and then assigned to the left operand.C /= A is equivalent to C = C / A
%=Assignment after modulus, the operands on both sides are taken modulus and then assigned to the left operand.C %= A is equivalent to C = C % A
<<=Bitwise left shift followed by assignmentC <<= 2 Is equivalent to C = C << 2
>>=Bitwise right shift followed by assignmentC >>= 2 Is equivalent to C = C >> 2
&=Bitwise AND operation followed by assignmentC &= 2 Is equivalent to C = C & 2
^=Bitwise XOR operation followed by assignmentC ^= 2 Is equivalent to C = C ^ 2
|=Bitwise OR operation followed by assignmentC |= 2 Is equivalent to C = C | 2

Online 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 );
   }
}

When the above code is executed, the output result 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

The precedence of operators depends on the operator group they belong to, which affects the calculation of the expression.

Example: x = 7 + 3 * 2; Here, the calculation result of x is 13), not 20, because multiplication (*), which is higher than addition (+), so it calculates first 3*2 In addition to 7.

See the following table, the priority decreases from top to bottom, the topmost has the highest priority, and the comma operator has the lowest priority.

CategoriesOperatorsAssociativity
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