zoukankan      html  css  js  c++  java
  • Scala 关键字

    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 个关键字,下面是大致归类的 Scala 关键字列表:

    • 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 共有的关键字,在两个语言里的含义也基本相同。只有一个例外: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 在 Java 的基础上迈出了一大步,从语法层面彻底消灭了基本类型,也就是说,一切皆是对象,Java 中所有的基本类型在 Scala 中都有相应的类:Boolean、Byte、Char、Short、Int、Long、Float和Double。具体请看这篇文章

    • void

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

    • interface和implements

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

    • static

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

    • public

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

    • const 和 goto

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

    • throws

    Scala 抛弃了 Checked Exception,以及 throws 关键字。在 Scala 里,借用了模式匹配的思想来做异常的匹配,因此,在 catch 的代码里,是一系列 case 字句,如下例所示:

    object Test {
       def main(args: Array[String]) {
          try {
             val f = new FileReader("input.txt")
          } catch {
             case ex: FileNotFoundException =>{
                println("Missing file exception")
             }
             case ex: IOException => {
                println("IO Exception")
             }
          }
       }
    }
    
    • native、transient、volatile和strictfp

    取代这4个关键字的,是4个注解:@native、@transient、@volatile 和 @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

    Scala 中取而代之的是 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,所以这两个关键字也解放了。
    在 Scala 循环中需要实现 break 和 continue 功能代码如下:

        // break
        breakable(
          for (i <- 0 until 10) {
            println(i)
            if (i == 5) {
              break()
            }
          }
        )
    
        // continue
        for (i <- 0 until 10) {
          breakable {
            if (i == 3 || i == 6) {
              break
            }
            println(i)
          }
        }
    
    作者:Binge
    本文版权归作者和博客园共有,转载必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。
  • 相关阅读:
    MySQL "show users"
    MySQL
    A MySQL 'create table' syntax example
    MySQL backup
    MySQL show status
    Tomcat, pathinfo, and servlets
    Servlet forward example
    Servlet redirect example
    Java servlet example
    How to forward from one JSP to another JSP
  • 原文地址:https://www.cnblogs.com/binbingg/p/14153628.html
Copyright © 2011-2022 走看看