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:

##*Multiply signA * B The result of the operation is 200 /Division signB / A The result of the operation is 2%RemainderB % A The operation result is 0
OperatorDescriptionExample
+Plus signA + B The result of the operation is 30
- Minus sign A - B The result of the operation is -10
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) );
      
   }
}

Running example»

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 operators

The following table lists the relational operators supported by Scala.

Assume that variable A is 10 and B is 20:

OperatorDescriptionExample== 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##<>=< =Example
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 = false

Logical operators

The following table lists the logical operators supported by Scala.

Assume that variable A is 1 and B is 0:

Operator&&||!

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

Execute the above code, the output result is:

$ scalac Test.scala 
$ scala Test
a && b = false
a || b = true
!(a && b) = true

Bit operators

Bit operators are used to operate on binary bits,~ ,&,|,^ are respectively negation, bitwise AND, bitwise AND or, bitwise AND XOR operation, as shown in the following table:

DescriptionExample
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
pqp & qp | qp ^ q
00000
010 11
11110
10011

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:

##&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##^~<<>>>>>

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;           /* 215 = 1111 */
      println("a >> 2  = " + c );

      c = a >>> 2;          /* 215 = 0000 1111 */
      println("a >>> 2 = " + c );
   }
}

Execute 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 = 240
a >> 2  = 15
a >>> 2 = 15

Assignment operators

The following lists the assignment operators supported by the Scala language:

OperatorDescriptionExample
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 operatora << 2 The output result is 240, binary interpretation: 1111 0000
Right shift operatora >> 2 The output result is 15, binary interpretation: 0000 1111
Unsigned right shiftA >>>2 The output result is 15, binary interpretation: 0000 1111
##%=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 valueC <<= 2 is equivalent to C = C << 2>>= Shift right bitwise and then assign the value C >>= 2 is equivalent to C = C >> 2##&=^=|=

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 >>= 2 ;
      println("c >>= a  = " + c );

      c &= a ;
      println("c &= 2  = " + c );
     
      c ^= a ;
      println("c ^= a  = " + c );

      c |= a ;
      println("c |= a  = " + c );
   }
}

Execute the above code, 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  = 1
c &= 2  = 0
c ^= a  = 10
c |= a  = 10

Operator precedence

An expression may contain multiple different Data objects of different data types connected by operators; since expressions have multiple operations, different operation orders may produce different results or even incorrect operation errors, because when an expression contains multiple operations, they must be Only by combining in a certain order can the rationality of the operation and the correctness and uniqueness of the results be ensured.

The priorities decrease from top to bottom, with the top one having the highest priority and the comma operator having the lowest priority.

In the same priority, the calculation is based on the order of combination. Most operations are calculated from left to right, and only three precedence levels are combined from right to left. They are unary operators, conditional operators, and assignment operators.

Basic priorities need to be remembered:

  • Pointers are optimal, and monocular operations are better than binocular operations. Such as plus and minus signs.

  • First multiply and divide (modulo), then add and subtract.

  • Arithmetic operation first, then shift operation, and finally bit operation. Please pay special attention to: 1 << 3 + 2 & 7 is equivalent to (1 << (3 + 2))&7

  • Final calculation of logical operation

OperatorDescriptionInstance
= 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
Assignment after bitwise AND operationC &= 2 is equivalent to C = C & 2
bitwise XOR operator and then assignmentC ^= 2 is equivalent to C = C ^ 2
bitwise OR operation and then assignmentC |= 2 is equivalent In C = C | 2
Operator typeOperatorCombining 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