要点:

使用Scala解释器

使用val和var定义变量

数字类型

使用操作符和函数

浏览Scaladoc

1、scala解释器

1、下载文件scala2.11.6.tgz

        http://www.scala-lang.org/download/2.11.6.html

2、登陆linux

       [root@master usr]#

 创建文件夹存放scala

       [root@master usr]# mkdir scala

上传文件到usr下面并解压

      [root@master usr]# tar -zxf scala-2.11.6.tgz

移动到scala下面

     [root@master usr]# mv scala-2.11.6 scala

3、编辑配置文件添加scala的配置

    [root@master scala-2.11.6]# vim /etc/profile

export SCALA_HOME=/usr/scala/scala-2.11.6

export PATH=$PATH:$SCALA_HOME/bin

:wq      #保存并退出

执行配置文件

[root@master scala-2.11.6]# . /etc/profile

4、验证scala

[root@master scala-2.11.6]# scala -version

Scala code runner version 2.11.6 -- Copyright 2002-2013, LAMP/EPFL

[root@master scala-2.11.6]# scala

Welcome to Scala version 2.11.6 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_71).

Type in expressions to have them evaluated.

Type :help for more information.

scala> 9*9

res0: Int = 81

注意:Table键补全功能

2、声明值和变量

格式: val/var 变量[:类型] = 值

比如:val greeting:String = null

         val greeting:Any = "Hello"

  val xmax,ymax =100 //将xmax和ymax赋值100

  var a = 1

  b = 2

val定义的值是常量,无法改变内容

val定义的值是变量,可以进行多次修改

3、常用类型

scala有7种数值类型:

Byte、Char、Short、Int、Long、Float、Double

RichInt、RichDouble、RichChar

BigInt、BigDecimal

Boolean

4、算术和操作符重载

scala的算术操作符和在java或者c++的效果是一样的

调用方法区别

a + b 是如下方法调用的简写a.+(b)

+是方法名

格式:

a  方法  b

作为以下代码的简写:

a.方法(b)

5、调用函数和方法

scala没有静态方法,不过它有一个类似的特性,叫做单例对象。通常一个类对应有一个半生对象,其方法就跟java中的静态方法一样。

比如:BigInt类的BigInt伴生对象有一个生产指定位数的随时素数的方法

probablePrime:

BigInt.probablePrime(100,scala.util.Radom)

6、apply方法

练习:

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 RichInt → ScalaNumberProxy

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.take\drop\takeRight和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方便。