scala 关键字

2018/04/28 10:12
阅读数 1.1K

Java 关键字

Java 一共有 50 个关键字(keywords),其中有 2 个是保留字,目前还不曾用到:goto 和 const。true、false 和 null 看起来很像关键字,但实际上只是字面量而已。本文粗略的把 true、false 和 null 也看做 Java 关键字,认为 Java 一共有 53 个关键字。下面是大致归类的 Java 关键字列表:

  • assert
  • boolean, byte, short, char, int, long, float, double, void
  • package, import, class, interface, enum, implements, extends
  • public, protected, private, abstract, static, final, volatile, transient, synchronized, strictfp, native
  • try, catch, finally, throw, throws
  • if, else, do, while, for, switch, case, default, break, continue, return
  • super, this
  • new, instanceof
  • const, goto
  • true, false, null

Scala 关键字

Scala 只有 39 个关键字:

  • package, import, class, object, trait, extends, with, type, forSome
  • private, protected, abstract, sealed, final, implicit, lazy, override
  • try, catch, finally, throw
  • if, else, match, case, do, while, for, return, yield
  • def, val, var 
  • this, super
  • new
  • true, false, null

对比 Java 和 Scala 关键字

为了直观的对比 Java 和 Scala 的关键字列表,我画了下面这张图:

Java 和 Scala 共有的关键字

Java 和 Scala 共有的关键字,在两个语言里的含义也基本相同。只有一个例外:case。在 Java 里,case 主要用在 switch-case 语句里。Scala 没有 switch-case 语句,case 关键字主要用来定义 case 类和进行模式匹配。

只有 Java 才用到的关键字

共有 28 个 Java 关键字没有被 Scala 采用,但 Scala 也是要运行在 JVM 上的,所以下面来看一下这 28 个关键字所表达的含义是如何在 Scala 里实现的。

assert

scala.Predef 定义了 assert () 方法,如下所示:

object Predef {
    @elidable(ASSERTION)
    def assert(assertion: Boolean) {
        if (!assertion)
            throw new java.lang.AssertionError("assertion failed")
    }

    @elidable(ASSERTION) @inline
    final def assert(assertion: Boolean, message: => Any) {
        if (!assertion)
            throw new java.lang.AssertionError("assertion failed: "+ message)
    }
}

boolean, byte, char, short, int, long, float, double

Scala 没有 primitive 类型,取而代之的是相应的类:Boolean、Byte、Char、Short、Int、Long、Float 和 Double。具体请看这篇文章

void

和 primitive 类型类似,Scala 使用了更加 OO 的方式来表达 void:Unit 类。具体请看这篇文章

interface 和 implements

Scala 用 Trait 取代了接口,具体请看这篇文章

static

Scala 没有 static 概念,取而代之的是单例对象。具体请看这篇文章

public

在 Scala 里,类、方法、字段等,默认就是 public,而且 Scala 也不允许将它们显式设置为 public,所以 public 这个单词没有任何特殊含义,可以自由使用。

const 和 goto

const 和 goto 不再是保留字,可以自由使用。

throws

Scala 抛弃了 Checked Exception,以及 throws 关键字。

native、transient、volatile 和 strictfp

取代这 4 个关键字的,是 4 个注解:scala.native、scala.transient、scala.volatile 和 scala.annotation.strictfp。下面是示例代码:

@transient var t = 1
@volatile var v = 1

@strictfp
def f() = {}

@native
def n(x: Int): Int;

synchronized

Scala 提供了 synchronized 方法,用起来几乎和 Java 的 synchronized 关键字一样:

def sync() = {
    this.synchronized {
        val x = 2
    }
}

但实际上并没有什么 synchronized 方法,反编译之后,上面的 Scala 代码和下面的 Java 代码一模一样:

public void sync() {
    synchronized(this) {
        ...
    }
}

instanceof

取而代之的是 isInstanceOf [] 方法,代码如下所示:

def instanceof(arg: Any) = {
    if (arg.isInstanceOf[String]) {
        val str = arg.asInstanceOf[String]
    }
}

enum

在 Scala 里,如果想定义枚举,应该继承 scala.Enumeration,比如下面这段代码:

object Color extends Enumeration {
    val Red = Value
    val Green = Value
    val Blue = Value
}

break, continue, default, switch

如前所述,Scala 用模式匹配取代了 switch-case,从而解放了 switch 和 default 关键字。而 Scala 的 for 循环和 Java 的 for 循环也大相径庭。Scala 虽然有 while 和 do-while 循环,但是里面不能用 break 和 continue,所以这两个关键字也解放了。