5.scala编程思想笔记——数据类型
欢迎转载,转载请标明出处:http://blog.csdn.net/notbaron/article/details/50444100
源码下载连接请见第一篇笔记。
Scala会区分不同类型的值。
例如
数字加上字符串,如下:
scala> 5.4+"Sally"
res0: String = 5.4Sally
Scala会将两个值连起来称为一个新的String来保存。
如果相乘则,scala会进行报错。
如下:
scala> 5.4*"Sally"
<console>:11: error: overloadedmethod value * with alternatives:
(x:Double)Double <and>
(x:Float)Double <and>
(x:Long)Double <and>
(x:Int)Double <and>
(x:Char)Double <and>
(x:Short)Double <and>
(x:Byte)Double
cannot be applied to (String)
5.4*"Sally"
^
也可以直接指定类型如下:
val name:type = initialization
例如:
val n:Int = 1
例子如下:
val whole:Int = 11
val fractional:Double = 1.4
// true or false:
val trueOrFalse:Boolean = true
val words:String = "A value"
val lines:String = """Triple quoteslet
you have many lines
in your string"""
println(whole, fractional,
trueOrFalse,words)
println(lines)
然后执行如下:
(11,1.4,true,A value)
Triple quotes let
you have many lines
in your string
例如:其中Int数据类型表示的是integer,意味着只能保存整数
Scala使用类型推断来确定混合使用类型情况下所表示的类型。
Scala会执行大量的类型推断,为程序员分忧。