1. 變量定義與初始化
package com.lineshen.chapter2
object variable {
def main(args: Array[String]): Unit = {
val age: Int = 10
val sal: Double = 10.9
val name: String = "lienshen"
val isPass: Boolean = true
val score: Float = 99.55f
println(s"${name} ${age} ${score} ${sal}")
}
}
變量類型可以寫可以不寫,與golang不同的是,定義類型用 [:type];
當然,scala支持變量類型的推導;
變量申請時,請注意初始化,這一點與java很不同;
val修飾的變量編譯之後等於加上了final,保證線程安全;
類型判斷與Java一樣用 variableName.theInstanceof[variableType];
2.數據類型
Scala與Java有着相同的數據類型,在Scala中數據類型都是對象,也就是說Scala沒有java中的原生類型。
Scala的數據類型分爲兩大類:AnyVal(值類型)和AnyRef(引用類型),注意:不管是AnyVal還是AnyRef都是對象。因此,他有大量的方法可以直接調用。
package com.lineshen.chapter2
object removePara {
def main(args: Array[String]): Unit = {
sayHi()
sayHi
}
def sayHi() : Unit ={
println("Hi, scala")
}
}
>>Hi, scala
>>Hi, scala
當函數沒有形參時,函數的調用可以免去括號(),真的很神奇!
小結:
1. 在Scala中有一個根類Any,是所有數據類型的父類
2. Null類型是scala的特別數據類型,只有一個值null;他是bottom class,是所有anyRef類型的子類
3. Nothing類型也是bottom class,它是所有數據類型對應類的子類,一般拋出異常時最常使用。
4. Scala中低精度向高精度的值自動隱式轉換
5. Unit表示無值,與其它語言中的void 等同,用作不反悔任何結果的方法的結果類型。Unit只是一個實例值,寫成();
package com.lineshen.chapter2
object nothingCTest {
def main(args: Array[String]): Unit = {
sayHello
}
// 異常中斷:即當我們nothing作爲返回值,就明確說明該方法沒有正常返回值
def sayHello() :Nothing = {
throw new Exception("拋出異常")
}
}
val floatVal : Float = 1.1 // 錯誤:Double無法向Float轉換
val doubleVal = 1.2f //正確:Float可以向Double轉換
3. 基本數據類型與String之間的轉換
// 基本數據類型 -> String
val temp :Int = 1024
val str = temp + ""
// String -> 基本數據類型
str.toInt()
str.toFloat()
str.toDouble()
str.toByte()
4. 分支與循環
package com.lineshen.chapter4
import scala.io.StdIn
object ifelseDemo {
def main(args: Array[String]): Unit = {
println("your name:")
val name = StdIn.readLine()
if (name == "lienshen")
println("hello " + name)
else
println("you are " + name)
}
}
在scala中沒有swich多分支設計,採用模板match-case,功能十分強大。
關於for循環,變動很大,成爲for推導式。
package com.lineshen.chapter4
object forDemo {
def main(args: Array[String]): Unit = {
for(i <- 1 to 5) {
println("hello, Tsinghua University " + i)
}
}
}
package com.lineshen.chapter4
object forDemo {
def main(args: Array[String]): Unit = {
// 支持對集合的遍歷,和for range很像
val list = List("hello", "tsinghua", "hello", "lineshen")
for (i <- list) {
println(i)
}
}
}
循環守衛,也叫循環保護式。類似於continue,不過scala把continue幹掉了(實質是把for循環與判斷偶合在了一起,簡化代碼)
package com.lineshen.chapter4
object forDemo {
def main(args: Array[String]): Unit = {
// 支持對集合的遍歷,和for range很像
val list = List("hello", "tsinghua", "hello", "lineshen")
for (i <- list if i == "lineshen") {
println(i)
}
}
}
for引入新變量(實質是for循環與遍歷變量的耦合)
package com.lineshen.chapter4
object forDemo {
def main(args: Array[String]): Unit = {
// 支持對集合的遍歷,和for range很像
val list = List("hello", "tsinghua", "hello", "lineshen")
for (i <- list; j = i + " i love you") {
println(j)
}
}
}
for簡化循環嵌套(實質是簡畫代碼長度,但是不好寫業務邏輯)
package com.lineshen.chapter4
object forDemo {
def main(args: Array[String]): Unit = {
// 支持對集合的遍歷,和for range很像
val list_i = List("hello", "tsinghua", "hello", "lineshen")
val list_j = List(" happy")
for (i <- list_i; j <- list_j) {
println(i+j)
}
}
}
for循環返回值yield (遍歷過程中把結果保存在Vector中,並返回到變量中);主要用於集合中的數據單元處理
package com.lineshen.chapter4
object forDemo {
def main(args: Array[String]): Unit = {
// 支持對集合的遍歷,和for range很像
val list = List("hello", "tsinghua", "hello", "lineshen")
val res = for (i <- list) yield i
println(res)
}
}
package com.lineshen.chapter4
object forDemo {
def main(args: Array[String]): Unit = {
// 支持對集合的遍歷,和for range很像
val list = List("hello", "tsinghua", "hello", "lineshen")
val res = for (i <- list) yield if (i == "lineshen") i + " happy"
println(res)
}
}
>> List((), (), (), lineshen happy)
for循環的步長控制(可以用守衛,可以用range)
package com.lineshen.chapter4
object forDemo {
def main(args: Array[String]): Unit = {
// 支持對集合的遍歷,和for range很像
val list = List("hello", "tsinghua", "hello", "lineshen")
val res = for (i <- Range(0,3,2)) yield list(i)
println(res)
}
}
>> Vector(hello, hello)
for循環不支持break關鍵字,同樣採用循環守衛進行處理。
Scala提供了while循環,但是不支持採用while循環。