Scala编程基础7(Scala运算符)

Scala含有丰富的内置运算符,包括以下几种类型:

  • 算术运算符
  • 关系运算符
  • 逻辑运算符
  • 位运算符
  • 赋值运算符
接下来分别介绍一下各种运算符的使用。
1.算术运算符 下图列出了Scala支持的算术运算符(假定变量A=10,B=20):
Scala编程基础7(Scala运算符)
文章图片
image 举个例子:编辑一个Test1.scala的文本文件,内容如下:
object Test1 { 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("a % b = " + (a % b)); } }

编译并运行Test1.scala,结果如下:
E:\Test>scalac Test1.scala
E:\Test>scala Test1
a + b = 30
a - b = -10
a * b = 200
a / b = 0
a % b = 10
2.关系运算符 下图列出了Scala支持的关系运算符(假定变量A=10,B=20):
Scala编程基础7(Scala运算符)
文章图片
image 举个例子:编辑一个Test2.scala的文本文件,内容如下:
object Test2 { 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) ); } }

编译并运行Test2.scala文件,结果如下:
E:\Test>scalac Test2.scala
E:\Test>scala Test2
a == b = false
a != b = true
a > b = false
a < b = true
a >= b = false
a <= b = true
3.逻辑运算符 下图列出了Scala支持的逻辑运算符(假定变量A=1,B=0):
Scala编程基础7(Scala运算符)
文章图片
image 举个例子:编辑一个Test3.scala的文本文件,内容如下:
object Test3 { 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) ); } }

编译并运行Test3.scala文件,结果如下:
E:\Test>scalac Test3.scala
E:\Test>scala Test3
a && b = false
a || b = true
!(a && b) = true
4.位运算符 位运算符用来对二进制位进行操作,位运算符包括:按位与&,按位或|,按位非~,按位异或^四种。它们的运算规则如下图所示:
Scala编程基础7(Scala运算符)
文章图片
image 举个例子:编辑一个Test4.scala的文本文件,内容如下:
object Test4 { 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 ); } }

【Scala编程基础7(Scala运算符)】编译并运行Test4.scala文件,结果如下:
E:\Test>scalac Test4.scala
E:\Test>scala Test4
a & b = 12 a | b = 61 a ^ b = 49
~a = -61 a << 2 = 240 a >> 2 = 15 a >>> 2 = 15
5.赋值运算符 下图列出了 Scala 支持的赋值运算符:
Scala编程基础7(Scala运算符)
文章图片
image 举个例子:编辑一个Test5.scala的文本文件,内容如下:
object Test5 { 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 ); } }

编译并运行Test5.scala文件,结果如下:
E:\Test>scalac Test5.scala
E:\Test>scala Test5
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
运算符优先级取决于所属的运算符组,它会影响算式的的计算。例如: x = 7 + 3 * 2; 这里, x 计算结果为 13, 而不是 20,因为乘法() 高于加法(+), 所以它先计算 32 再加上 7。查看以下表格,优先级从上到下依次递减,最上面具有最高的优先级,逗号操作符具有最低的优先级。
Scala编程基础7(Scala运算符)
文章图片
image

    推荐阅读