基础
代码语言:javascript复制 def primitiveType(): Unit = {
//scala没有原始类型,都是对象
println("1.toString -> " 1.toString)
//富类型自动转换Int->RichInt再调用to
println("1.to(199) -> " 1.to(199))
println(""Hello.intersect("low")" -> " "Hello".intersect("low"))
//a.方法(b) == a 方法 b
println("1.to(199) -> " (1 to 199))
//scala没有 或者--
}
def callFunction = {
println("sqrt(2) -> " sqrt(2))
println("pow(2,4) -> " pow(2, 4))
println("min(3,Pi)" min(3, Pi))
println(""jsjsjdaj".distinct -> " "jsjsjdaj".distinct)
}
def applyMethod: Unit = {
//字符串字符定位
println(""hello"(4) -> " "hello" (4))
println(""hello".apply(4) -> " "hello".apply(4))
//构建对象常用方法
println("BigInt("12344567890") -> " BigInt("1234567890"))
println("Array(1,2,3,4,5,6,7,8,9,0) -> " Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0))
}
def exercise: Unit = {
println(pow(sqrt(3.0), 2.0))
println(""asdasd" * 3 -> " ("asdasd" * 3))
println("10 max 2 -> " (10 max 2))
println("BigInt("2") pow 1024 -> " (BigInt("2") pow 1024))
println("probablePrime(10, Random) -> " probablePrime(10, Random))
println("随机文件名:" BigInt(Random.nextInt()).toString(36))
val str = "abcdefghijklmn"
println("str(0) -> " str(0))
println("str.take(0) -> " str.take(1))
println("str.reverse(0) -> " str.reverse(0))
println("str.takeRight(0) -> " str.takeRight(1))
}
输出:
代码语言:javascript复制1.toString -> 1
1.to(199) -> Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199)
"Hello.intersect("low")" -> lo
1.to(199) -> Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199)
sqrt(2) -> 1.4142135623730951
pow(2,4) -> 16.0
min(3,Pi)3.0
"jsjsjdaj".distinct -> jsda
"hello"(4) -> o
"hello".apply(4) -> o
BigInt("12344567890") -> 1234567890
Array(1,2,3,4,5,6,7,8,9,0) -> [I@2bc7c6c3
2.9999999999999996
"asdasd" * 3 -> asdasdasdasdasdasd
10 max 2 -> 10
BigInt("2") pow 1024 -> 179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216
probablePrime(10, Random) -> 811
随机文件名:aa52hj
str(0) -> a
str.take(0) -> a
str.reverse(0) -> n
str.takeRight(0) -> n
控制结构和函数
{}块是有值的,值就是最后一个表达式的值;没有值的表达式(比如说赋值类型的)值为Unit
代码语言:javascript复制def value = {
val a = {
val b = 1
b
}
println("a->" a)
var d = 0
val c = d = 1;
println("c -> " c)
}
输出:
代码语言:javascript复制a->1
c -> ()
条件表表达式有值,值就是各个分支,缺少分支为Unit,完整分支为所有分支类型
代码语言:javascript复制 def conditionExpression: Unit = {
val cond = 1
val a =
if (cond == 1)
true
else
false
println(a)
}
def conditionType: Unit = {
val cond = 1
var a = //Unit类型
if (cond == 0)
1
//上面的相当于
a =
if (cond == 0)
1
else
()
val b = //Any类型
if (cond == 2)
1
else
"hello"
println("a -> " a)
println("b -> " b)
}
输出:
代码语言:javascript复制true
a -> ()
b -> hello
scala没有受检异常,throw表达式类型为Nothing
代码语言:javascript复制def handleException: Unit = {
//scala没有受检异常
//throw表达式类型为Nothing
println("---------------------------------------------")
val cond = 1
try {
val a =
if (cond == 0)
true
else if (cond < 0)
throw new IllegalArgumentException("cond is less than 0!")
else
throw new Exception("cond is larger than 0!")
} catch {
case e1: IllegalArgumentException => e1.printStackTrace()
case e2: Exception => e2.printStackTrace()
} finally {
println("finally block!")
}
//对于条件分支,如果一个分支返回throw表达式,那么它的类型就是其他分支的类型
}
输出:
代码语言:javascript复制---------------------------------------------
java.lang.Exception: cond is larger than 0!
at com.hash.learn.scala.Chapter2.exception$.handleException(exception.scala:21)
at com.hash.learn.scala.Chapter2.CMain$.main(CMain.scala:25)
at com.hash.learn.scala.Chapter2.CMain.main(CMain.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
finally block!
参数
代码语言:javascript复制 def parameter: Unit = {
//带名参数
def decorate(left: String = "[", middle: String, right: String = "]") = left middle right
println(decorate(middle = "asd"))
//匿名参数
def decorate2(middle: String, left: String = "[", right: String = "]") = left middle right
println(decorate2("asd"))
//变长参数(输入为Seq)
def sum(args: Int*) = {
var sum = 0
for (i <- args) {
sum = i
}
sum
}
println(sum(1, 9, 9, 9))
//Range to Seq
println(sum(1 to 9: _*))
}
输出:
代码语言:javascript复制[asd]
[asd]
28
45
循环
代码语言:javascript复制def whileLoop: Unit = {
var n = 10
while (n > 0) {
println(n)
n -= 1
}
}
def forLoop: Unit = {
//to 为闭区间
for (i <- 1 to 5)
println(i)
//until为左闭右开区间
for (i <- 1 until 5)
println(i)
println("---------------------------------------------")
//可以用字符串
for (i <- "abcdefg")
println(i)
}
def advanceForLoop: Unit = {
//嵌套循环写在同一行
println("---------------------------------------------")
for (i <- 1 to 3; j <- 1 to 3)
println("i:" i " j:" j)
//可以加条件
println("---------------------------------------------")
for (i <- 1 to 3; j <- 1 to 3 if i != j)
println("i:" i " j:" j)
//可以有中间变量
println("---------------------------------------------")
for (i <- 1 to 10; from = i % 3; j <- 1 to from)
println("i:" i " j:" j)
//for推导式
println(for (i <- 1 to 10) yield i)
println(for (i <- 1 to 10) yield i % 3)
//推导式生成的集合类型和第一个生成器类型兼容
println(for (i <- "abcdefg"; j <- 1 to 2) yield i)
println(for (j <- 1 to 2; i <- "abcdefg") yield i)
}
输出:
代码语言:javascript复制10
9
8
7
6
5
4
3
2
1
1
2
3
4
5
1
2
3
4
---------------------------------------------
a
b
c
d
e
f
g
---------------------------------------------
i:1 j:1
i:1 j:2
i:1 j:3
i:2 j:1
i:2 j:2
i:2 j:3
i:3 j:1
i:3 j:2
i:3 j:3
---------------------------------------------
i:1 j:2
i:1 j:3
i:2 j:1
i:2 j:3
i:3 j:1
i:3 j:2
---------------------------------------------
i:1 j:1
i:2 j:1
i:2 j:2
i:4 j:1
i:5 j:1
i:5 j:2
i:7 j:1
i:8 j:1
i:8 j:2
i:10 j:1
Vector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
Vector(1, 2, 0, 1, 2, 0, 1, 2, 0, 1)
aabbccddeeffgg
Vector(a, b, c, d, e, f, g, a, b, c, d, e, f, g)