快学Scala-第一章 基础

知识点:

Scala程序并不是一个解释器,实际发生的是,你输入的内容被快速的编译成字节码,然后这段字节码交由Java虚拟机执行。

以val定义的值是一个常量,以var定义的值是一个变量,声明值或变量但不做初始化会报错。

变量或函数的类型总是写在变量或函数名称的后面。

Scala中,仅当同一行代码中存在多条语句时才需要用分号隔开。

Scala有7种数值类型:Byte、Char、Short、Int、Long、Float、Boolean,这些类型是类。在Scala中不需要包装类型,在基本类型和包装类型之间的转换是Scala编译器的工作,比如,Scala用底层的java.lang.String类表示字符串,通过StringOps类给字符串加很多操作。类似,还有RichInt、RichDouble、RichChar提供了原始类不具备的方法。BigInteger和BigDecimal对应的是java.math.BigInteger java.math.BigDecimal.

Scala没有++和--,需要+=1和-=1.Java中不能对操作符重载,在Scala中是可以定义操作符的。

Scala中调用函数时,一般没有参数且不改变当前对象的方法不带圆括号。

练习:(参考了网上的答案呢,自己敲一遍练一下)

1.在Scala REPL中键入3.,然后按TAB键,有哪些方法可以被应用?

scala> 3.
!=   >             floatValue      isValidInt     to               toRadians

%    >=            floor           isValidLong    toBinaryString   toShort

&    >>            getClass        isValidShort   toByte           unary_+

*    >>>           intValue        isWhole        toChar           unary_-

+    ^             isInfinite      longValue      toDegrees        unary_~

-    abs           isInfinity      max            toDouble         underlying

/    byteValue     isNaN           min            toFloat          until

<    ceil          isNegInfinity   round          toHexString      |

<<   compare       isPosInfinity   self           toInt

<=   compareTo     isValidByte     shortValue     toLong

==   doubleValue   isValidChar     signum         toOctalString

2.计算3的平方根,再求平方,计算这个值离3差多少。

scala> scala.math.sqrt(3)
res0: Double = 1.7320508075688772
scala> res0*res0
res1: Double = 2.9999999999999996
scala> 3-res1
res2: Double = 4.440892098500626E-16

3.res变量时var还是val?

试一下不就知道了~~给res重新赋值出现错误,说明是val。

4.Scala允许你用数字去乘字符串——试一下“crazy”*3。在Scaladoc中如何找到这个操作?

scala> "hello"*3
res3: String = hellohellohello

说明“*”是字符串的一个方法,首先是一个String,所以直接查找StringOps类,然后搜索“*”,即可找到啦。

def *(n: Int): String
Return the current string concatenated n times.

5.10 max 2 的含义是什么?max方法定义在哪个类中?

scala> 10 max 2
res4: Int = 10

根据书中的查找DOC的经验,首先是数值类型,在左侧栏搜索 int ,在右侧栏Int类的介绍下的搜索框搜索 max就可以找到相应的解释。

def max(that: Int): Int
returns this if this > that or that otherwise

Definition Classes RichIntScalaNumberProxy

6.用BigInt计算2的1024次方。

scala> BigInt(2).pow(1024)
res5: scala.math.BigInt = 179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216

7.为了在使用probablePrime(100,Random)获取随机素数时不在probablePrime和Random之前使用任何限定符,你需要引入什么?

import scala.math.BigInt._; import scala.util.Random

8.创建随机文件的方式之一是生成一个随机的BigInt,然后将他转换成三十六进制,输出类似“qsnvbevtomcj38o06kul”这样的字符串,查阅scaladoc,找到实现该逻辑的办法。(首先要导入BigInt和Random)

scala> BigInt(Random.nextInt).toString(36)
res11: String = -121h7l

9.在Scala中如何获取字符串的首字符和尾字符?

scala> "Scala"(0)
res15: Char = S
scala> "Scala".take(1)
res16: String = S
scala> "Scala".reverse(0)
res17: Char = a
scala> "Scala".takeRight(1)
res18: String = a

10.takedrop akeRight和dropRight这些字符串函数是做什么用的?和substring相比,他们的优点和缺点都有哪些?

def take(n: Int): String
Selects first n elements.
def drop(n: Int): String
Selects all elements except first n ones.

def takeRight(n: Int): String
Selects last n elements.
def dropRight(n: Int): String
Selects all elements except last n ones.

scala> "Scala".take(3).drop(1)
res19: String = ca

如上的四个方法都是单向求取其中的子字符串,如果需要求中间的字符,则需要用两个函数结合起来,没有subString方便。

原文地址:https://www.cnblogs.com/yiruparadise/p/5512887.html