Latest web development tutorials

# Scala operator

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

Scala rich built-in operators, including the following types:

• Arithmetic operators

• Relational Operators

• Logical Operators

• Bitwise Operators

• Assignment Operators

Next, we will explain to you more than the application of various operators.

## Arithmetic operators

The following table lists the Scala support arithmetic operators.

A variable assumed as 10, B 20:

Operators description Examples
+ plus A + B operation result is 30
- Minus A - B operation result is -10
* Multiplication sign A * B operation result is 200
/ Sign of division B / A operation result is 2
% Remainder B% A calculation result is 0

### Examples

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

}
}
```

Implementation of the above code, the output 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 Scala support relational operators.

A variable assumed as 10, B 20:

Operators description Examples
== equal (A == B) Operation result is false
! = not equal to (A! = B) operation result is true
> more than the (A> B) Operation result is false
< Less than (A <B) operation result is true
> = greater or equal to (A> = B) the operation result is false
<= Less than or equal (A <= B) operation result is true

### Examples

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

Implementation of the above code, the output 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 Scala supports logical operators.

A variable assumed as 1, B is 0:

Operators description Examples
&& Logic and (A && B) Operation result is false
|| Logical or (A || B) operation result is true
! Logical NOT ! (A && B) operation result is true

### Examples

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

Implementation of the above code, the output is:

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

## Bitwise Operators

Bitwise Operators bits used to operate, ~, &, |, ^ were negated, and the bit, or by bit and, bitwise XOR operation with the following table examples:

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; two variables corresponding to binary is:

```A = 0011 1100

B = 0000 1101

-------位运算----------

A&B = 0000 1100

A|B = 0011 1101

A^B = 0011 0001

~A  = 1100 0011
```

The Scala Bitwise rule as follows:

Operators description Examples
& Bitwise AND operator (A & b) output 12, binary explanation: 0000 1100
| Bitwise OR operator (A | b) output 61, binary explanation: 00111101
^ Bitwise exclusive OR operator (A ^ b) the output of 49, binary explanation: 00110001
~ Bitwise Operators (~ A) -61 output, binary explanation: 1100 0011 in a symbolic form complement binary number.
<< Left mobile operator a << 2 outputs 240 Binary interpretation: 11110000
>> Right Mobile Operators a >> 2 output 15, binary explanation: 0000 1111
>>> Unsigned right shift A >>> 2 outputs 15 binary explanation: 0000 1111

### Examples

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

Implementation of the above code, the output 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 Scala language support assignment operator:

Operators description Examples
= Simple assignment operator, specifies the right operand assigned to the left operand. C = A + B A + B operation result is assigned to C
+ = Adding after the assignment, the left and right sides of the operand before adding assigned to the left operand. C + = A corresponds to C = C + A
- = After subtraction assignment, the left and right operands on both sides of the left operand after subtraction assigned to. C - = A corresponds to C = C - A
* = Then multiplying the assignment, the left and right sides of the operands are multiplied before assignment to the left operand. C * = A corresponds to C = C * A
/ = Divided after the assignment, the left and right sides of the dividing operand after assignment to the left operand. C / = A is equivalent C = C / A
% = Remainder after the assignment, the left and right sides of the remainder operand after assignment to the left operand. C% = A is equivalent to C = C% A
<< = Bitwise left after assignment C << = 2 corresponds to C = C << 2
>> = Bitwise right after the assignment C >> = 2 is equivalent to C = C >> 2
& = Bitwise AND assignment after operation C & = 2 is equivalent to C = C & 2
^ = Bitwise exclusive OR operator and then assigned C ^ = 2 is equivalent to C = C ^ 2
| = Bitwise OR assignment after C | = 2 corresponds to C = C | 2

### Examples

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

Implementation of the above code, the output 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

In an expression may contain a plurality of different operators connecting with different data types of data objects; since the expression has a variety of operations, a different order of operations may have different results even erroneous operation error, because when when an expression containing a variety of operations, according to a certain order must be combined in order to ensure the rationality and correctness of the results, the uniqueness of operation.

Priority in descending order from top to bottom, the top has the highest priority, the comma operator has the lowest priority.

The same priority level, according to the binding order of calculation. Most operations are calculated from left to right, only three priorities are right-to-left combination, they are unary operators, conditional operators, assignment operators.

The basic priorities need to remember:

• Optimal pointer, unary than binary operators. As the sign.
• First multiplication and division (modulus) after subtraction.
• First arithmetic shift operation after the last bit computing. Please pay particular attention to: << 1 2 & 3 + 7 is equivalent to (1 << (3 + 2)) & 7
• Final calculation logic operation
Operator Type Operators Binding direction
Expression evaluation () []. Expr ++ expr-- Left to Right
Unary operator

* & + -! ~ ++ Expr --expr

* /%

+ -

>> <<

<> <=> =

==! =

Right to Left
Bitwise Operators

&

^

|

&&

||

Left to Right
Ternary operator ?: Right to Left
Assignment Operators = + = - = * = / =% = >> = << = & = ^ = | = Right to Left
comma , Left to Right