一、安裝scala地址:https://www.scala-lang.org/download/,經過sjava
二、在idea中編譯運行。添加IDEA Scala(執行此操做後,pom文件中不用添加scala依賴,應爲已經以lib庫的方式加入)shell
進入Module Setting或者按F4進入界面,選擇Gloabal Library,右鍵scala jar包,選擇Add to Modules... 便可。express
配置好環境變量後,shell輸入scala。sass
一、基本操做語句session
scala> 3+4 res0: Int = 7 scala> 4*5+8 res1: Int = 28
二、Tab自動補全方法app
scala> "hello,scala" res2: String = hello,scala scala> res2.to to toBoolean toByte toDouble toIndexedSeq toIterable toList toLowerCase toSeq toShort toString toUpperCase toArray toBuffer toCharArray toFloat toInt toIterator toLong toMap toSet toStream toTraversable toVector scala> res2.sort sortBy sortWith sorted scala> res2.sorted res3: String = ,aacehlllos
三、REPL模式(read-eval-print-loop)dom
四、查看命令行,以冒號開頭的都是命令操做ide
scala> :help All commands can be abbreviated, e.g., :he instead of :help. :completions <string> output completions for the given string :edit <id>|<line> edit history :help [command] print this summary or command-specific help :history [num] show the history (optional num is commands to show) :h? <string> search the history :imports [name name ...] show import history, identifying sources of names :implicits [-v] show the implicits in scope :javap <path|class> disassemble a file or class name :line <id>|<line> place line(s) at the end of history :load <path> interpret lines in a file :paste [-raw] [path] enter paste mode or paste a file :power enable power user mode :quit exit the interpreter :replay [options] reset the repl and replay all previous commands :require <path> add a jar to the classpath :reset [options] reset the repl to its initial state, forgetting all session entries :save <path> save replayable session to a file :sh <command line> run a shell command (result is implicitly => List[String]) :settings <options> update compiler options, if possible; see reset :silent disable/enable automatic printing of results :type [-v] <expr> display the type of an expression without evaluating it :kind [-v] <type> display the kind of a type. see also :help kind :warnings show the suppressed warnings from the most recent line which had any
一、val值不可變,var值可變。函數
scala> val answer = 1 answer: Int = 1 scala> var not = false not: Boolean = false
二、必要的時候能夠聲明類型:變量:類型 = 值oop
scala> var greeting: String = null greeting: String = null scala> val s1,s2:String = "test" s1: String = test s2: String = test
Byte Char Short Int Long Float Double
RichInt(Double、Char)
StringOps
a 方法 b
a.方法(b)
不支持 ++ --
若是沒有參數,就不須要使用括號。
若是一個無參方法並不修改對象,調用時就能夠不用寫括號。
引入包的方式 _ 相似於java的*
能夠直接使用半生對象的方法。
使用以scala開頭的包時,能夠省去scala的前綴。
一、相似函數調用的語法:
s(i) <=> java中的s.charAt(i),C++中的s[i] scala> val s = "abc" s: String = abc scala> s(2) res9: Char = c scala> s[2] <console>:1: error: identifier expected but integer literal found. s[2]
背後的實現邏輯:apply
二、一樣,以下的操做都是背後調用了操做對象所屬類的apply方法
scala> BigInt("123456") res10: scala.math.BigInt = 123456 scala> BigInt.apply("123456") res12: scala.math.BigInt = 123456 scala> Array(1,2,3) res13: Array[Int] = Array(1, 2, 3) scala> Array.apply(1,2,3) res14: Array[Int] = Array(1, 2, 3)
相似於JavaDoc同樣的文檔查看系統。
一、在scala中輸入3.
,會提示那些方法可使用?
scala> 3. != + << >= abs compareTo getClass isNaN isValidChar isWhole round to toDegrees toInt toShort underlying % - <= >> byteValue doubleValue intValue isNegInfinity isValidInt longValue self toBinaryString toDouble toLong unary_+ until & / == >>> ceil floatValue isInfinite isPosInfinity isValidLong max shortValue toByte toFloat toOctalString unary_- | * < > ^ compare floor isInfinity isValidByte isValidShort min signum toChar toHexString toRadians unary_~
二、在Scala REPL中,計算3的平方根,而後再對該值求平方。如今,這個結果與3相差多少?
scala> import scala.math._ import scala.math._ scala> sqrt(3) res15: Double = 1.7320508075688772 scala> pow(res15,2) res16: Double = 2.9999999999999996 scala> 3 - res16 res18: Double = 4.440892098500626E-16
三、res變量是val仍是var?
scala> res18 = res18 + 1 <console>:15: error: reassignment to val res18 = res18 + 1
顯然是不可變的,即val。
四、Scala容許用數字乘以字符串,去REPL中試一下"crazy" * 3.這個操做作了什麼?在ScalaDoc中如何找到這個操做?
scala> "crazy" * 3 res19: String = crazycrazycrazy
這個操做將字符串疊加了3次,造成一個新的字符串返回;在ScalaDoc中StringOps類中能夠找到該方法
def *(n: Int): String Return the current string concatenated n times.
五、 10 max 2
的含義是什麼?max定義在哪一個類中? 含義爲返回10和2中值最大的那一個。也能夠寫爲10.max(2)、2.max(10)
scala> 10 max 2 res0: Int = 10
定義在Int類中
def max(that: Int): Int Returns this if this > that or that otherwise.
六、用BigInt計算2的1024次方。
scala> BigInt(2).pow(1024) res8: scala.math.BigInt = 179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216
七、爲了在使用probablePrime(100,Random)
獲取隨機質數時不在probablePrime和Radom以前使用任何限定符,須要引入什麼?
// Random引用 scala> import scala.util._ import scala.util._ // probablePrime引用 scala> import scala.math.BigInt._ import scala.math.BigInt._ scala> probablePrime(100,Random) res8: scala.math.BigInt = 960697016320705319171295980203
八、建立隨機文件的方式之一輩子成一個隨機的BigInt,而後將他轉換成36進制,返回相似於"qsnvbevtomcj38o06kul"這樣的字符串。查閱scala文檔找到實現該邏輯的方法。
scala> val num = BigInt.probablePrime(100,Random) num: scala.math.BigInt = 661712999120439539288883430513 scala> num.toString(36) res9: String = 1s5jrbb731snvh11spdd
九、在scala中如何獲取字符串的首字符和尾字符。
scala> var s9 = "abcde" s9: String = abcde scala> s9(0) res10: Char = a scala> s9.last res10: Char = e
十、take、drop、takeRight和dropRight這些字符串方法是作什麼用的?和substring相比,他們的優勢和缺點有什麼呢?
1. 在StringLike中 * take:Selects first n elements.(選擇開頭的n個字符) * takeRight :Selects last n elements.(選擇末尾的n個字符) 2. StringOps drop :Selects all elements except first n ones. (選擇除了開頭的n個字符) dropRight:Selects all elements except last n ones. (選擇除了末尾的n個字符) substring: 選擇子串,這個要構造一個新的字符串