云星数据---Scala实战系列(精品版)】:Scala入门教程012-Scala实战源码-Scala操作符

183 篇文章 0 订阅
64 篇文章 1 订阅

Scala 操作符

package scala_learn.demo00_helloworld

/**
 * Created by liguohua on 2017/2/12.
 */
/*
scala的操作符和java是一致的
 */
object Operator_03 {

  def test0(): Unit = {
    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))
  }

  def test1(): Unit = {
    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))
  }

  def test2(): Unit = {
    var a = true
    var b = false
    println("!(a && b) = " + !(a))
    println("a && b = " + (a && b))
    println("a || b = " + (a || b))
  }

  def test3(): Unit = {
    var a = 60 /* 60 = 0011 1100 */
    var b = 13 /* 13 = 0000 1101 */
    var c = 0 /* 0  = 0000 0000 */
    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 (左补0)*/
    println("a >>> 2 = " + c)
  }

  def test4: Unit = {
    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)
  }

}

scala运算符实质

scala本质无运算符,所有运算均为函数调用
  • +等价于.+()
  • a+b等价于a.+(b)
  • -等价于.-()
  • a-b等价于a.-(b)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值