目錄java
不能使用保留字.數據庫
val xyz_++= = 1
能夠經過反引號定義標識符,兩個反引號內的任意長度的字符串即是定義的標識符.ide
def `test that addition work`= assert(1 + 1 = 2)
List(1, 2, 3, 4).filter((i: Int) => isEven(i)).foreach((i: Int) => println(i)) List(1, 2, 3, 4).filter(i => isEven(i)).foreach(i => println(i)) List(1, 2, 3, 4).filter(isEven).foreach(println) List(1, 2, 3, 4) filter isEven foreach println
1. 全部字母 2. | 3. ^ 4. & 5. < > 6. = ! 7. : 8. + - 9. * / % 10. 其餘特殊字符
::
方法將某一元素放置到列表前面scala> val list = List('c', 'd') list: List[Char] = List(c, d) scala> 'a' :: list res1: List[Char] = List(a, c, d) scala> val list = List('c', 'd') list: List[Char] = List(c, d) scala> 'b' :: list res2: List[Char] = List(b, c, d) scala> list.::('a') res3: List[Char] = List(a, c, d) scala> 'a' :: list res4: List[Char] = List(a, c, d) scala> 'b' :: list res5: List[Char] = List(b, c, d)
注意:this
'a' :: list
等價於 list.::('a')
編碼
不能使用該方法重複插入多個字符es5
Scala的if語句能夠向Java中同樣使用scala
if (2 + 2 = 5) { println("if") } else if (2 + 2 = 3) { println("else if") } else { println("else") }
Scala中的if語句是有返回值的. 能夠將if表達式的結果值賦值給其餘變量. 如下面代碼爲例.code
val configFile = new java.io.File("someFile.txt") val configFilePath = if (configFile.exists()) { configFile.getAbsolutePath() } else { configFile.createNewFile() configFile.getAbsolutePath() }
if語句返回值的類型也被稱爲全部條件分支的最小上界類型,也就是與每條each子句可能返回值類型最接近的父類型.在上面例子中,configFilePath
是if表達式的結果值,該if表達式將執行文件不存在的條件分支(假定),並返回新建立文件的絕對路徑.將if語句的返回值賦予變量configFilePath
.orm
Scala不支持三元表達式對象
val list = List("A", "B", "C") for (item <- list) println(item)
A B C
item <- list
這樣的表達式被稱爲生成器表達式val list = List("ABC", "CDE", "EFG") for (item <- list if item.contains("C") ) println(item)
ABC CDE
val list = List("ABC", "BCD", "CDE") for (item <- list if item.contains("B") if !item.startsWith("A") ) println(item) for (item <- list if item.contains("B") && !item.startsWith("A") ) println(item)
BCD BCD
yielding
關鍵字能在for表達式中生成新的集合val list = List("ABC", "BCD", "CDE") val filterItems = for {item <- list if item.contains("B") if !item.startsWith("A") } yield item filterItems
scala> val list = List("ABC", "BCD", "CDE") list: List[String] = List(ABC, BCD, CDE) scala> val filterItems = for {item <- list | if item.contains("B") | if !item.startsWith("A") | } yield item filterItems: List[String] = List(BCD)
val list = List("abc", "bcd", "cde") val upcaseItems = for { item <- list upcaseItem = item.toUpperCase() } yield upcaseItem
scala> val list = List("abc", "bcd", "cde") list: List[String] = List(abc, bcd, cde) scala> | val upcaseItems = for { | item <- list | upcaseItem = item.toUpperCase() | } yield upcaseItem upcaseItems: List[String] = List(ABC, BCD, CDE)
while (表達式) { do something }
var count = 0 do { count += 1 } while(count < 10)
操做符 | 操做 |
---|---|
&& | 和操做 |
|| | 或操做 |
> | 大於 |
>= | 大於或等於 |
< | 小於 |
<= | 小於或等於 |
== | 等於 |
!= | 不等於 |
&&
和 ||
是短路操做符
在Java中==
只會對對象引用進行比較, 調用equals
來比較字段值; Scala使用==
符執行邏輯上的相等檢查. 即調用equals
方法進行判斷.
public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public boolean equals(Object obj) { return true; } public static void main(String args[]) { Person p1 = new Person("sun", 20); Person p2 = new Person("li", 20); System.out.println(p1 == p2); } } // 輸出: false
class Person(var name: String, var age: Int) { override def equals(obj: scala.Any): Boolean = true } val p1 = new Person("sun", 20) val p2 = new Person("sun", 20) println(p1 == p2) // 輸出: true
case NonFatal(ex) => ...
子句使用scala.util.control.NonFatal
匹配了全部的非致命性異常throw new MyBadException(...)
拋出異常try { ... } catch { case NonFatal(ex) => println(s"Non fatal exception! $ex") case ... } finally { ... }
object ExpensiveResource { lazy val resource: Int = init() def init(): Int = { // 某些代價昂貴的操做 0 } }
// file: enum.scala object Breed extends Enumeration { type Breed = Value val doberman = Value("Doberman Pinscher") val yorkie = Value("Yorkshire Terrier") val scottie = Value("Scottish Terrier") val dane = Value("Great Dane") val portie = Value("Portuguese Water Dog") } import Breed._ println("ID\tBreed") for (breed <- Breed.values) println(s"${breed.id}\t$breed") println("\nJust Terriers:") Breed.values filter (_.toString.endsWith("Terrier")) foreach println def isTerrier(b: Breed) = b.toString.endsWith("Terrier") println("\nJust Terriers:") Breed.values filter isTerrier foreach println
scala enum.scala
ID Breed 0 Doberman Pinscher 1 Yorkshire Terrier 2 Scottish Terrier 3 Great Dane 4 Portuguese Water Dog Just Terriers: Yorkshire Terrier Scottish Terrier Just Terriers: Yorkshire Terrier Scottish Terrier
下面是一個簡單示例:
val name = "Buck Trends" println(s"Hello, $name")
下面是幾個對可插入字符串進行格式化的示例.
scala> val gross = 100000F gross: Float = 100000.0 scala> val net = 64000F net: Float = 64000.0 scala> val percent = (net / gross) * 100 percent: Float = 64.0 scala> println(f"$$${gross}%.2f vs. $$${net}%.2f or ${percent}%.1f%%") $100000.00 vs. $64000.00 or 64.0% scala> val s = "%02d: name = %s".format(5, "Dean Wampler") s: String = 05: name = Dean Wampler // 原生插入器 scala> val name = "Dean Wampler" name: String = Dean Wampler scala> s"123\n$name\n456" res1: String = 123 Dean Wampler 456 scala> raw"123\n$name\n456" res2: String = 123\nDean Wampler\n456
$$
打印一個$
%%
打印一個%