Scala语言基础-变量、数据类型、表达式

1. 变量

1.1 三种变量修饰符

  1. val 定义 immutable variable
  2. var 定义 mutable variable
  3. lazy val
    注:可以不显示指定变量类型,scala会自动进行类型推导

    1.2 实例

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
scala> val x = 10
x: Int = 10

scala> val y:Int = 10
y: Int = 10

scala> x + x
res0: Int = 20

scala> res0
res1: Int = 20

scala> val z = 20
z: Int = 20

scala> z = 10 //val常量不可变
<console>:11: error: reassignment to val
z = 10
^

scala> var d = 20
d: Int = 20

scala> d = 10
d: Int = 10

scala> val m = 20
m: Int = 20

scala> val n = 30
n: Int = 30

scala> lazy val p = m * n
p: Int = <lazy>

scala> p * 10
res3: Int = 6000

scala> p //lazy 定义量初次使用时被加载,当定义的变量在后续可能不被使用时添加lazy修饰符
res5: Int = 600

2. 数据类型

2.1 类型体系

2.2 实例

2.2.1 AnyVal

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
scala> val a:Byte = 10
a: Byte = 10

scala> val b:Short = 20
b: Short = 20

scala> val c:Int = 30
c: Int = 30

scala> val d:Long = 40
d: Long = 40

scala> val e:Float = 50
e: Float = 50.0

scala> val f:Double = 60.12345
f: Double = 60.12345

scala> val x:Long = b
x: Long = 20

scala> val y:Byte = x //高精度不可向低精度转化
<console>:12: error: type mismatch;
found : Long
required: Byte
val y:Byte = x
^

scala> val m = true
m: Boolean = true

scala> val n = false
n: Boolean = false

scala> val q = 'X'
q: Char = X

scala> val u:Unit = ()
u: Unit = ()

scala> val p = ()
p: Unit = ()

scala> def foo() = throw new Exception("error")
foo: ()Nothing

2.2.2 String

  1. 构建于Java的String之上
  2. 新增了字符串插值(interpolation)的特性
    1
    2
    val myName = "Humbinal" //creating a String
    s"My name is ${myName}"

3. 代码块、表达式

3.1 代码块(Block)

1
2
3
4
5
{exp1;exp2}
{
exp1
exp2
}

Block也是一个表达式,其最终求得的值是最后一个表达式的值

3.2 if与for

3.2.1 if表达式

1
if (logical_exp)  valA else valB

实例:

1
2
3
4
5
6
7
if (true) 1 else 2                              //> res2: Int = 1
if (false) 3 else 4 //> res3: Int = 4

val a=1 //> a : Int = 1
if (a==1) a //> res4: AnyVal = 1
if (a!=1) "not one" //> res5: Any = ()
if (a!=1) "not one" else a //> res6: Any = 1

3.2.2 for comprehension

1
2
3
4
5
for{
x <- xs
y = x+1
if (y>0)
} yield y

实例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
val l = List("aa","bbb","cccc")

for(
s<- l //generator
) println(s)

for(
s<- l
if (s.length > 3) //filter
) println(s)


val result_for = for {
s <- l
s1 = s.toUpperCase() //variable binding
if(s1 != "")
} yield (s1) //generate new collection

3.3 try与match表达式

3.3.1 try表达式

1
2
3
try{}
catch{}
finally{}

实例:

1
2
3
4
5
6
7
8
try {
Integer.parseInt("dog")
}catch{
case _ => 0
} finally{
println("always be printed")
} //> always be printed
//| res7: Int = 0

3.3.2 match表达式

1
2
3
4
5
6
exp match { //主要用在patten match 中
case p1 => val1
case p2 => val2
...
case _ => valn
}

实例:

1
2
3
4
5
6
val code = 3                                    //> code  : Int = 3
val result_match = code match {
case 1 => "one"
case 2 => "two"
case _ => "others"
} //> result_match : String = others

0%