scala基础学习(一)

scala学习

scala与java的不同之处:

1、scala中任何一个操作符都是一个方法。

  s = 1+2;    可以写作:s=(1).+(2)

2、异常捕获采用模式匹配的方式。

try {
 val f = new FileReader("input.txt") // Use and close file 
} catch { 
case ex: FileNotFoundException => // Handle missing file
 case ex: IOException => // Handle other I/O error
 }

3、并且scala中try-finally语句中产生返回值,但是finally中最好用于关闭连接等必须要完成的事。

4、match匹配(没有continue以及break语句)

val firstArg = if (args.length > 0)   args(0)  else ""
 firstArg match { 
    case "salt" => println("pepper") 
    case "chips" => println("salsa") 
    case "eggs" => println("bacon")
    case _ => println("huh?")
}

5、scala中没有++、--的用法。

6、变量范围(在局部变量中可以重新定义变量)

val a = 1;
 { 
    val a = 2    // 编译通过
}
原文地址:https://www.cnblogs.com/moss-yang/p/7223585.html