Scala学习笔记-5-类层次关系和一些常用方法

底层类型

scala.Null 和 scala.Nothing
类 Null 代表 null 引用。它是所有引用类(每个由 AnyRef 派生的类)的子类,但是 Null 和值类型不兼容。(任何引用类都可以赋值为 null,但是值类型不能赋值为 null)
Nothing 类型是所有其他类的子类,然而这个类型没有任何实例(也就是没有任何值对应 Nothing 类型),所以任何方法的返回值都可以是 Nothing(常用于异常分支)

常用方法

package test

object Useful {

  def main(args: Array[String]): Unit = {

    // 将两个数组转换成二元组的数组
    Array(1, 2, 3) zip Array("a", "b")

    // 可以应用到任何序列数据结构
    Array(1, 2, 3) mkString " - "

    // 数组连接
    Array(1, 2, 3) ++ Array("a", "b")

    42 max 43
    42 min 43
    -1.abs
    42.hashCode
    1 until 5
    1 to 5

    // eq等同于Java中的==,比较的是引用
    // equals和==等同于 Java中的equals,比较的是值
    println(new Integer(1) eq new Integer(1))
    println(new Integer(1) == new Integer(1))
    println(new Integer(1) equals new Integer(1))

  }
}
posted @ 2019-12-03 14:10  飞_2016  阅读(111)  评论(0编辑  收藏  举报