快學Scala-第一章 基礎

知識點:html

Scala程序並非一個解釋器,實際發生的是,你輸入的內容被快速的編譯成字節碼,而後這段字節碼交由Java虛擬機執行。java

以val定義的值是一個常量,以var定義的值是一個變量,聲明值或變量但不作初始化會報錯。api

變量或函數的類型老是寫在變量或函數名稱的後面。dom

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.this

Scala沒有++和--,須要+=1和-=1.Java中不能對操做符重載,在Scala中是能夠定義操做符的。es5

Scala中調用函數時,通常沒有參數且不改變當前對象的方法不帶圓括號。spa

練習:(參考了網上的答案呢,本身敲一遍練一下)scala

1.在Scala REPL中鍵入3.,而後按TAB鍵,有哪些方法能夠被應用?code

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.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方便。

相關文章
相關標籤/搜索