Scala operator


Release date:2023-11-14 Update date:2023-11-15 Editor:admin View counts:270

Label:

Scala operator

An operator is a symbol that tells the compiler to perform specified mathematical and logical operations.

Scala contains a wealth of built-in operators, including the following types:

  • Arithmetic operator

  • Relational operator

  • Logical operator

  • Bit operator

  • Assignment operator

Next, we will introduce the application of the above operators in detail.

Arithmetic operator

The following table lists the arithmetic operators supported by Scala.

Suppose the variable An is 10 and B is 20:

Operator

Description

Example

+

Plus sign

The result of A + B operation is 30

-

Minus sign

The result of A-B operation is-10

*

Multiplication sign

The result of A * B operation is 200

/

Division sign

The result of B / A operation is 2.

%

Take the remainder

B% A operation result is 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) );

   }
}

Execute the above code, and the output is as follows:

$ scalac Test.scala
$ scala Test
a + b = 30
a - b = -10
a * b = 200
b / a = 2
b % a = 0
c % a = 5

Relational operator

The following table lists the relational operators supported by Scala.

Suppose the variable An is 10 and B is 20:

Operator

Description

Example

==

Equal to

(a = = B) the result is false

! =

Not equal to

(a! = B) the result is true

>

Greater than

(a > B) the result is false

<

Less than

(a < B) the result is true

> =

Greater than or equal to

(a > = B) the result is false

< =

Less than or equal to

(a < = B) the result is 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) );
   }
}

Execute the above code, and the output is as follows:

$ scalac Test.scala
$ scala Test
a == b = false
a != b = true
a > b = false
a < b = true
b >= a = true
b <= a = false

Logical operator

The following table lists the logical operators supported by Scala.

Suppose the variable An is 1 and B is 0:

Operator

Description

Example

& &

Logic vs.

(a & B) the result is false

||

Logical OR

(a|| B) the result is true

!

Logic is not

! (a & B) the result is 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) );
   }
}

Execute the above code, and the output is as follows:

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

Bit operator

The bit operator is used to operate on binary bits. ~, &, |, ^ are inverted, respectively, by bit and, by bit OR, by bit XOR, 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 you specify A = 60; and B = 13; the binary corresponding to the two variables is:

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 algorithm in Scala is as follows:

Operator

Description

Example

&

Bitwise and operator

(a & b) output result 12, binary explanation: 0000 1100


Bitwise or operator

(a | b) output 61, binary explanation: 0011 1101

^

Bitwise XOR operator

(a ^ b) output 49, binary explanation: 0011 0001

~

Bitwise inverse operator

(~ a) output result-61, binary interpretation: 1100 0011, in the form of a complement of signed binary numbers.

< <

Left move operator

A < < 2 output result 240. binary explanation: 1111 0000

> >

Right move 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[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 );
   }
}

Execute the above code, and the output is as follows:

$ 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 operator

The assignment operators supported by the Scala language are listed below:

Operator

Description

Example

=

A simple assignment operation that specifies that the right Operand is assigned to the left Operand.

C = A + B assigns the operation result of A + B to C.

+=

Add and then assign values, add the operands on the left and right sides, and then assign values to the operands on the left.

C + = An is equivalent to C = C + A

-=

Subtract and then assign a value, subtract the left and right operands and then assign a value to the left Operand.

C-= An is equivalent to C = C-A

*=

Multiply and then assign values, multiply the operands on the left and rightsides and then assign values to the operands on the left.

C *= A Equivalent to C = C * A

/=

Divide and assign values, divide the left and right operands and then assignvalues to the left Operand.

C /= A equivalent to C = C / A

%=

After the remainder, the Operand on the left and the left is assigned to theoperand on the left.

C% = A is equivalent to C = C% A

<<=

Assign values after bitwise left shift

C <<= 2 equivalent to C = C << 2

>>=

Move bitwise to the right before assigning

C >>= 2 equivalent to C = C >> 2

&=

Bitwise and post-operation assignment

C &= 2 equivalent to C = C & 2

^=

Assign a value after a bitwise XOR operator

C ^= 2 equivalent to C = C ^ 2

|=

Assign values after bitwise or operation

C |= 2 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 );
   }
}

Execute the above code, and the output is as follows:

$ 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 priority of the operator depends on the group of operators to which it belongs, which affects the evaluation of the expression.

Example: X = 7 + 3 * 2; Here, the result of x calculation is 13, not 20, because multiplication(* ) is higher than addition (+), so it calculates 3 times 2 and then adds 7.

Looking at the table below, the priority decreases from top to bottom, with the highest priority at the top and the lowest priority for the comma operator.

Category

Operator

Relevance

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

Powered by TorCMS (https://github.com/bukun/TorCMS).