Programming in Scala (Second Edition) 读书笔记5

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***的方法

时间: 2024-10-20 22:23:08

Programming in Scala (Second Edition) 读书笔记5的相关文章

Programming in Scala (Second Edition) 读书笔记21 隐式转化

1. There's a fundamental difference between your own code and libraries of other people: you can change or extend your own code as you wish, but if you want to use someone else's libraries, you usually have to take them as they are. 你可以改变或扩展自己的代码,但是对

Programming in Scala (Second Edition) 读书笔记13 packages and import

目前,在一个包中你看到的top level的对象只有:class, trait, object.其实任何对象都可以是top level的.也就是说,没有必要把函数,value, variable等限制在class, trait, object中.它们可以在整个包范围内都是全局性的. 方法很简单,把这些东东放到package object中就行了.pakcage object的名字和包名相同 包含package object的文件放在该packag下,名字就叫package.scala chapt

Programming in Scala (Second Edition) 读书笔记12 Trais

1.什么是Trait ? Traits are a fundamental unit of code reuse in Scala. A trait encapsulatesmethod and field definitions, which can then be reused by mixing them intoclasses. Unlike class inheritance, in which each class must inherit from justone supercla

Programming in Scala (Second Edition) 读书笔记15 case class and pattern matching

一个算术表达式包含: 数字,变量,二元操作符,一元操作符.用下面几个类来模拟它们 package chapter15 abstract class Expr case class Var(name: String) extends Expr case class Number(num: Double) extends Expr case class UnOp(operator: String, arg: Expr) extends Expr case class BinOp(operator: 

Programming in Scala (Second Edition) 读书笔记10

你肯定见过在控制台用字符打印图形的程序,这一章就从定义一个图形元素开始.我们的图形元素都是一些字符组成的矩形 abstract class Element {   def contents: Array[String]   def height: Int = contents.length   def width: Int = if (height == 0) 0 else contents(0).length } 上面定义的三个方法都没有参数,连小括号也省去了.这样的方法叫做:无参方法(par

Programming in Scala (Second Edition) 读书笔记6 函数和闭包

When programs get larger, you need some way to divide them into smaller, more manageable pieces. For dividing up control flow, Scala offers an approach familiar to all experienced programmers: divide the code into functions. In fact, Scala offers sev

Programming in Scala (Second Edition) 读书笔记7 内置控制结构

1. One thing you will notice is that almost all of Scala's control structures result in some value Scala的每种控制语句都是有值的 This is the approach taken by functional languages, in which programs are viewed as computing a value, thus the components of a progr

Programming in Scala (Second Edition) 读书笔记26 Extractors

1. By now you have probably grown accustomed to the concise way data can be decomposed and analyzed using pattern matching. This chapter shows you how to generalize this concept further. Until now, constructor patterns were linked to case classes. Fo

Programming in Scala (Second Edition) 读书笔记3

创建并初始化一个String Array     val strArr1 = new Array[String](3)     strArr1(0) = "How"     strArr1(1) = "are"     strArr1(2) = "you!" 上述代码被transform 为     val strArr1 = new Array[String](3)     strArr1.update(0, "How")

Programming in Scala (Second Edition) 读书笔记4

1.Tuple和List一样都是不可变对象(immutalbe),但是Tuple的元素可以是不同的类型     val myTuple = ("A", 1, "park", 3.13359 )     println(myTuple._1 )  // A     println(myTuple._2)  // 1     println(myTuple._3)  // park 创建Tuple只需把元素放在括号里面就可以了,上面创建了一个Tuple4类的实例,Tup