Scala学习手册(Learning Scala) 第二章

Scala学习手册(Learning Scala) 第二章

1、值与变量定义

值的定义

值,即为常量,不可变,基本语法定义:

val : = 

创建一个名为a,类型为Int的值,并赋值为10

scala> val a:Int = 5
a: Int = 5
scala> a = a/4
:12: error: reassignment to val       
a = a/4         ^

注释:值的不可变性,导致不能将变化的值赋给自己。

变量定义

变量,是可变的,语法定义为:

var : = 

创建一个变量b,类型为Double的值,并赋值为5.1

scala> var b:Double = 5.1
b: Double = 5.1

scala> b = 5.1 / 2
b: Double = 2.55

如果在未指定类型,也就是没有":"时,Scala编译器会根据所赋的数据来推导它的类型。

scala> val c = 5.3
c: Double = 5.3

scala> val c1 = 5
c1: Int = 5

scala> val c2 = "hello world!"
c2: String = hello world!

变量

scala> var c = 5.3
c: Double = 5.3

scala> var c1 = 5
c1: Int = 5

scala> var c2 = "hello world!"
c2: String = hello world!

值与变量比较

值保持全局的不变性,使得数据具有稳定性和可预测性。

2、命名

字母后跟0个字母或多个字母和数字,如word5344

字母后跟0个字母或多个字母和数字,然后是一个下划线(_),后面是一个或多个字母和数字,或者一个或多个操作字符。如rwe34_tr$

一个或多个操作字符

3、数据类型

Scala支持的数据类型有:Byte、Short、Int、Long、Float、Double

支持向上转换,如Int类型到Long,但不支持Long到Int,因为转换空间限制,容易出现丢失数据的情况发生。

scala> val a:Int = 5
a: Int = 5

scala> val b:Long = a
b: Long = 5

scala> val c:Long = 10
c: Long = 10

scala> val d:Int = c
:12: error: type mismatch;
 found   : Long
 required: Int
       val d:Int = c

但是可以使用toType方法来做转换。

scala> val c:Long = 10
c: Long = 10

scala> val d:Int = c.toInt
d: Int = 10

不同类型数据的转换函数如下:

toString,toInt,toDouble,toFloat,toLong

4、字符串和内插

使用双引号来表示,当有特殊字符需要用反斜线转义

scala> val hello:String = "hello world"
hello: String = hello world

scala> val signature:String = "With Regards,\nYour friends"
signature: String =
With Regards,
Your friends

Char字面量要使用单引号,String字符串使用双引号。&&和||,功能上类似于 或 ,只要第一个参数足以得出结论,他们就不会再去计算第二个参数;&和| ,类似于 且 ,则会在返回结果之前对两个参数都进行检查。

字符串相等,用"==",用于判断是否相等。

scala> val greeting:String = "hello world"
greeting: String = hello world

scala> val matched = (greeting == "hello world")
matched: Boolean = true

字符串内插,可用于参数引用。

多个字符串连接起来,用"+"

scala> val approx:Float = 355 / 113f
approx: Float = 3.141593

scala> println("Pi,using 335/113f,is about " + approx + ".")
Pi,using 335/113f,is about 3.141593.

在一个字符串内插入变量

需要在字符串前面加上一个"s"前缀,然后使用$指示外部数据的引用,也可以选用大括号。

scala> val approx:Float = 355 / 113f
approx: Float = 3.141593

scala> println(s"Pi,using 355/113,is about $approx.")
Pi,using 355/113,is about 3.141593.

如果引用与周围文本无法区分,就需要用大括号

scala> val item:String = "apple"
item: String = apple

scala> s"How do you like them ${item}s?"
res4: String = How do you like them apples?

scala> s"Fish n chips n vinegar,${"pepper "*3}salt"
res5: String = Fish n chips n vinegar,pepper pepper pepper salt

使用printf来控制数据格式化,比如字符个数、小数值的显示。

使用方法:需要将前缀改为"f"

scala> val item:String = "apple"
item: String = apple

scala> f"I wrote a new $item%.3s today"
res6: String = I wrote a new app today

scala> f"Enjoying this $item ${335/113.0}%.5f times today"
res9: String = Enjoying this apple 2.96460 times today

总结:

  • 若参数为数值类型,如Int、Float、Double,在传递参数时需要用f和%.xf,第一个f表示是格式化,第二个f表示浮点数
scala> val item:Double = 3.43
item: Double = 3.43

scala> f"How do you like them $item%.1f"
res43: String = How do you like them 3.4
  • 若参数为字符串
scala> val item:String = "3fdere"
item: String = 3fdere

scala> f"How do you like them $item%.3s"
res51: String = How do you like them 3fd

5、元组

包含两个或多个值的有序容器,如(5,"data","she")

创建元组,或者利用关系操作符-> 来表示键值对关系

scala> val info:(Int,String,Boolean) = (5,"Korben",true)
info: (Int, String, Boolean) = (5,Korben,true)

scala> val red = "red" -> "0xff0000"
red: (String, String) = (red,0xff0000)

scala> val reversed = red._2 -> red._1
reversed: (String, String) = (0xff0000,red)

元组索引

scala> val name = info._2
name: String = Korben

你可能感兴趣的:(Scala学习手册(Learning Scala) 第二章)