1.基本数据类型
Byte | 8-bit signed |
Short | 16-bit signed |
Int | 32-bit signed |
Long | 64-bit signed |
Float | 32-bit IEEE 754 single-precision float |
Double | 64-bit IEEE 754 double-precision float |
Boolean | true or false |
String | a sequence of chars |
2.字面值(literal)
05 | 八进制 结果为Int类型 |
0x5 | 十六进制 结果为Int类型 |
123L / 123l | 结果Long类型 |
1.2345 | 结果为Double类型 |
1.2345F / 1.2345f | 结果为Float类型 |
3e5 / 3e5D | 结果为Double类型 |
‘A‘ ‘\101‘ ‘\u0041‘ | 字符字面值 |
""" dfdsfdsfdsf sdfdsf """ "scala" |
字符串字面值 |
‘aSymbol | 相当于 Symbol("aSymbol") |
3. 操作符。
Any method can be an operator
In Scala operators are not special language syntax: any method can
be an operator. What makes a method an operator is how you use it.
When you write “s.indexOf(‘o‘)”, indexOf is not an operator. But
when you write “s indexOf ‘o‘”, indexOf is an operator, because
you’re using it in operator notation.
操作符是位于操作符位置的方法,方法是位于方法位置的操作符。方法与操作符在scala中本质上一样的。
除了常见的二元操作符以外,Scala还有一元操作符,一元操作符分两类
一类叫前缀操作符, 如 : -, ~ , 这类操作符翻译成方法后会自动加上unary_,如“-”被翻译为"unary_-"
一类叫后缀操作符,如: toString
4.Scala中的==
first check the left side for null, and if it is not null, call the equals
method. Since equals is a method, the precise comparison you get depends
on the type of the left-hand argument. Since there is an automatic null check,
you do not have to do the check yourself.
先检查左边是否为null,如果不为null,就调eqauls方法
5. 富包装 (rich wrapper)
You can invoke many more methods on Scala’s basic types than were described in the pr
evious sections. A few examples are shown in Table 5.4.
These methods are available via implicit conversions,
Scala为Int,Short等基本类型定义了更多的方法,你可以通过隐式转换调到它们
隐式转换是很强大的功能,以后再细讲,这里先给一张截图
可以看到, a 本来是 scala.Int类型的,却可以调到 scala.runtime.Rich***的方法