首先解释下字面量(literal),字面量就是在用户不指定变量类型的情况下,系统默认把变量定位某种类型,这种类型就是字面量。
整数字面量:
类型Int、Long、Short、Byte的整数字面量有3种格式:十进制,十六进制和八进制。以0x或0X开头的默认为16进制,以0开始的被认为8进制,在数字后加上l或L就被认为Long。如
scala> val hex = 0x5
hex: Int = 5
scala> val hex = 0x00FF
hex: Int = 255
scala> val hex = 0x00FF
hex: Int = 255
scala> val hex1=0xcafeba
hex1: Int = 13303482
scala> val oct=035
warning: there were 1 deprecation warning(s); re-run with -deprecation for details
oct: Int = 29
scala> val oct1=0777
warning: there were 1 deprecation warning(s); re-run with -deprecation for details
oct1: Int = 511
scala> val dec = 31
dec: Int = 31
scala> val dec1 = 12314
dec1: Int = 12314
scala> val long = 123l
long: Long = 123
scala> val long = 123L
long: Long = 123
其余类型的字面量赋值和java差不多,略过。