[Scala Basic Grammar] 07. Operators

16200287:

Scala language provides these kinds of operators: arithmetic operators, relational operators, logical operators, bitwise operators, assignment operators, etc.

insert image description here

Note: Actually, Scala has no operators in the traditional sense. In Scala, everything is an object, and an operator is a method defined on an object. In Scala, the actual operation behind an operator is a method call.

val a = 10
val b = 5
val result1 = a.+(b)
val result2 = a + b

It is equivalent to calling this method for object a +(), but generally the space will .be omitted.

1. Arithmetic operators

operator describe example
+ plus The result of A + B operation is 30
- minus sign A - B operation result is -10
* Multiplication sign A * B operation result is 200
/ division sign The result of B / A operation is 2
% Take the remainder B % A operation result is 0

Code example:

var a = 10
var b = 20
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 = " + (a % b))

// 输出
a + b = 30
a - b = -10
a * b = 200
b / a = 2
b % a = 0
c % a = 10

2. Relational operators

operator describe example
== equal (A == B) evaluates to false
!= not equal to (A != B) evaluates to true
> more than the (A > B) evaluates to false
< less than (A < B) evaluates to true
>= greater or equal to (A >= B) The result of the operation is false
<= less than or equal to (A <= B) operation result is true

Code example:

val a = 10
val 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))

// 输出
a == b = false
a != b = true
a > b = false
a < b = true
b >= a = true
b <= a = false

3. Logical operators

operator describe example
&& logic and (A && B) evaluates to false
|| logical or (A || B) evaluates to true
! logical NOT !(A && B) operation result is true

Code example:

val a = true
val b = false

println("a && b = " + (a && b))
println("a || b = " + (a || b))
println("!(a && b) = " + !(a && b))

// 输出
a && b = false
a || b = true
!(a && b) = true

4. Bitwise operators

operator describe example
& bitwise AND operation 0 & 1, the result is 0
| bitwise OR 0|1, the result is 1
~ bitwise negation ~12, the result is -13; ~(-12), the result is 11
^ bitwise XOR operation 1^1, the result is 0
<< left shift operation 2<<2, the result is 8
>> right shift operation 4>>2, the result is 1
>>> unsigned right shift operation 4>>>2, the result is 1

Code example:

val a = 60 /* 60 = 0011 1100 */

val 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)

// 输出
a & b = 12
a | b = 61
a ^ b = 49
~a = -61
a << 2 = 240
a >> 2  = 15
a >>> 2 = 15

5. Assignment operator

operator describe example
= A simple assignment operation, specifying that the right operand is assigned to the left operand. C = A + B Assign the operation result of A + B to C
+= Add and then assign, add the left and right operands together and assign to the left operand. C += A is equivalent to C = C + A
-= Subtract and then assign, subtract the left and right operands and then assign to the left operand. C -= A is equivalent to C = C - A
*= Multiply and then assign, the left and right operands are multiplied and then assigned to the left operand. C *= A is equivalent to C = C * A
/= Assign after division, divide the left and right operands and then assign to the left operand. C /= A is equivalent to C = C / A
%= After the remainder is assigned, the remainder of the left and right operands is assigned to the left operand. C %= A is equivalent to C = C % A
<<= Bitwise left shift and then assign C <<= 2 is equivalent to C = C << 2
>>= Bitwise right shift and then assign C >>= 2 is equivalent to C = C >> 2
&= Assignment after bitwise AND operation C &= 2 is equivalent to C = C & 2
^= Bitwise XOR operator and then assignment C ^= 2 is equivalent to C = C ^ 2
|= Bitwise OR operation and then assignment C |= 2 is equivalent to C = C | 2

Code example:

var a = 10
val 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)

// 输出
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

6. Operator precedence

In Scala, operator precedence follows rules similar to other programming languages. Scala's operator precedence, from high to low, is as follows:

  1. Unary operators: +, -, !, ~(bitwise negate)
  2. Arithmetic operators: *, /,%
  3. Bitwise operators: &, |,^
  4. Addition and subtraction operators: +,-
  5. Relational operators: <, >, <=, >=,instanceof
  6. Equality operator: ==,!=
  7. Logical operators: &&,||
  8. Assignment operators: =, +=, -=, *=, /=, %=, &=, |=,^=

When using multiple operators, operators with higher precedence are executed before operators with lower precedence. If you are unsure about precedence, you can use parentheses to explicitly specify the order of operations. For example:

val result = 2 + 3 * 4  // result的值为14,乘法的优先级高于加法
val value = (2 + 3) * 4  // value的值为20,使用括号明确指定先进行加法运算

In actual programming, it is recommended to explicitly specify the order of operations using parentheses to avoid confusion and errors. Use parentheses to make expressions more clear and understandable.

Guess you like

Origin blog.csdn.net/m0_47256162/article/details/132158669
Recommended