val myStr = "Hello Scala!" //> myStr : String = Hello Scala! val myStr2: String = "Hello Scala!" //> myStr2 : String = Hello Scala!
示例-1:
scala> val x = 10 # 定义常量x x: Int = 10 # Scala自动推导出x的类型是Int scala> val y:Int =20 # 显示定义Int类型常量 y: Int = 20 scala> x + x # 定义表达式 res0: Int = 20 # res0是Scala的REPL自动为没有变量名的表达式设置默认变量名,res是result的简写,0是默认变量的标号 scala> res0 # 直接引用表达式res0 res1: Int = 20 # 因为res0这个表达式没有变量名,所以REPL默认设置res1作为res0表达式结果的变量名 scala> res0*res1 res2: Int = 400 scala> val z=res0+res1+res2 # 为表达式指定变量名 z: Int = 440 scala> z = 100 # 为Z赋新值 <console>:12: error: reassignment to val # 常量Z的值不可以被改变 z = 100 ^ scala> var a = 200 # 定义变量a a: Int = 200 scala> a = 300 # 可以为变量a赋新值 a: Int = 300 scala> val d = 20 d: Int = 20 scala> val e = 30 e: Int = 30 scala> lazy val f = d * e # 定义惰性常量f f: Int = <lazy> # f的值没有立即求解,<lazy>是惰性的标记 scala> f*10 # 当惰性常量第一次被使用时才会被求值 res3: Int = 6000 scala> f res4: Int = 600 scala>
val x, y = 100 //> x : Int = 100 //| y : Int = 100 val test = (50, "demo") //> test : (Int, String) = (50,demo)
val t = 123 //123就是整数字面量 //> t : Int = 123 val t2 = 3.14 //3.14就是浮点数字面量 //> t2 : Double = 3.14 val t3 = true //true就是布尔型字面量 //> t3 : Boolean = true val t4 = ‘A‘ //‘A‘就是字符字面量 //> t4 : Char = A val t5 = "Hello" //"Hello"就是字符串字面量 //> t5 : String = Hello "abc".intersect("bcd") //> res0: String = bc
注意:intersect()方法用来输出两个字符串中都存在的字符
scala> val a:Byte = 10 a: Byte = 10 scala> val b:Short = 20 b: Short = 20 scala> val c:Int = 30 c: Int = 30 scala> val d:Long = 40 d: Long = 40 scala> val e:Float = 50 e: Float = 50.0 scala> val f:Double = 60.98765 f: Double = 60.98765 scala> val x:Long = b # Short类型常量b赋值给Long类型常量x,低精度赋值给高精度 x: Long = 20 scala> val y:Byte = x # Long类型常量x赋值给Byte类型常量y,高精度赋值给低精度 <console>:12: error: type mismatch; # 提示类型不匹配 found : Long required: Byte val y:Byte = x ^ scala>
scala> val m=true m: Boolean = true scala> val n=false n: Boolean = false scala>
scala> val u:Unit = () # 显式声明Unit类型常量,()是文字量 u: Unit = () scala> val p=() p: Unit = () scala>
scala> def foo() = throw new Exception("error occurred") foo: ()Nothing scala>
scala> ‘a‘ res0: Char = a scala> ‘\n‘ res1: Char = scala> ‘\t‘ res2: Char = scala> ‘test‘ <console>:1: error: unclosed character literal (or use " for string literal "test") ‘test‘ ^ scala>
val myStr = "Hello Scala!" //> myStr : String = Hello Scala! val myStr2: String = "Hello Scala!" //> myStr2 : String = Hello Scala! val myStr3 : java.lang.String = "Hello Scala!" //> myStr3 : String = Hello Scala!
scala> val myname = "Anliven" myname: String = Anliven scala> s"My name is ${myname}" res0: String = My name is Anliven scala> """row 1 | row 2 | row 3""" res1: String = row 1 row 2 row 3 scala>
print("This is a ") //> This is a print("test!") //> test! println("This is a test!") //> This is a test! val test = 123456 //> test : Int = 123456 println(test) //> 123456 printf("My name is %s, the ID is %d.\n", "Anliven", test) //> My name is Anliven, the ID is 123456.
val Sum1 = 6 + 2 //> Sum1 : Int = 8 val Sum2 = (6).+(2) //> Sum2 : Int = 8
在Scala中并没有提供“++”和“--”操作符,但可以“+=”方式来实现自增或自减
var test = 1 //> test : Int = 1 test += 1 println(test) //> 2
注意:使用“+=”方式时,要使用var声明变量,否则执行时会报错。
0 到 255 间的 Unicode 字符可以用一个八进制转义序列来表示,即反斜线?\?后跟 最多三个八进制。
println("Hello World\n\nHello Scala"); //> Hello World //| //| Hello Scala
1 to 3 //> res0: scala.collection.immutable.Range.Inclusive = Range 1 to 3 1.to(3) //> res1: scala.collection.immutable.Range.Inclusive = Range 1 to 3 1 until 3 //> res2: scala.collection.immutable.Range = Range 1 until 3 1 to 10 by 3 //> res3: scala.collection.immutable.Range = Range 1 to 10 by 3 0.5f to 5.9f by 0.8f //> res4: scala.collection.immutable.NumericRange[Float] = NumericRange 0.5 to 5. //| 9 by 0.8
scala> var x = 10 x: Int = 10 scala> var y = 20 y: Int = 20 scala> x + y res0: Int = 30 scala> {x*2;y*2;x*y} # 代码块也是一个表达式,其最终求得的值是最后一个表达式的值。 res1: Int = 200 scala> { | x + y | x + x + y + y | } res2: Int = 60 scala>
def functionName ([参数列表]) : [return type] = { function body return [expr] }
def hello(name: String): String = { s"hello, ${name}" } //> hello: (name: String)String hello("Anliven") //> res0: String = hello, Anliven def hello2(name: String) = { s"hello, ${name}" } //> hello2: (name: String)String hello2("Anliven") //> res1: String = hello, Anliven def add(x: Int, y: Int) = x + y //> add: (x: Int, y: Int)Int add (1,2) //> res2: Int = 3
if(布尔表达式) { // 如果布尔表达式为 true 则执行该语句块 }
if(布尔表达式){ // 如果布尔表达式为 true 则执行该语句块 }else{ // 如果布尔表达式为 false 则执行该语句块 }
一些示例:
if (true) 1 else 2 //> res0: Int = 1 if (true) 3 else 4 //> res1: Int = 3 val a = 1 //> a : Int = 1 if (a == 1) a //> res2: AnyVal = 1 if (a != 1) "not one" //> res3: Any = () # 表达式不返回值,结果就是Unit的文字量(), if (a != 1) "not one" else a //> res4: Any = 1
scala> val x = 6 x: Int = 6 scala> :paste // Entering paste mode (ctrl-D to finish) if (x>0) {println("This is a positive number") } else { println("This is not a positive number") } // Exiting paste mode, now interpreting. This is a positive number scala> scala> :paste // Entering paste mode (ctrl-D to finish) if (x>0) { println("This is a positive number") } else if (x==0) { println("This is a zero") } else { println("This is a negative number") } // Exiting paste mode, now interpreting. This is a positive number scala> scala> val test = if (x>0) 1 else -1 test: Int = 1 scala>
var x = 3 //> x : Int = 3 while (x > 0) { x -= 1 printf("i is %d\n", x) } //> i is 2 //| i is 1 //| i is 0 var y = 0 //> y : Int = 0 do { y += 1 println(y) } while (y < 3) //> 1 //| 2 //| 3
for (变量<-表达式) 语句块 // 其中,“变量<-表达式”被称为生成器(generator)
for (i <- 1 to 3) println(i) //> 1 //| 2 //| 3 for (i <- 1 to 3 by 2) println(i) //> 1 //| 3 for (i <- 1 to 3 if i % 2 == 0) println(i) //> 2 for (i <- 1 to 3; j <- 1 to 3) println(i * j) //> 1 //| 2 //| 3 //| 2 //| 4 //| 6 //| 3 //| 6 //| 9 for (i <- 1 to 3 if i % 2 == 0; j <- 1 to 3 if j != i) println(i * j) //> 2 //| 6 for (i <- 1 to 5 if i % 2 == 0) yield i //> res0: scala.collection.immutable.IndexedSeq[Int] = Vector(2, 4)
val test = List("Aaaa", "Bbb", "Cccc") //> test : List[String] = List(Aaaa, Bbb, Cccc) for ( demo <- test ) println(demo) //> Aaaa //| Bbb //| Cccc for { demo <- test if (demo.length > 3) } println(demo) //> Aaaa //| Cccc val result_for = for { demo <- test demo1 = demo.toUpperCase() if (demo1 != "") } yield (demo1) //> result_for : List[String] = List(AAAA, BBB, CCCC)
object first { val result_try = try { Integer.parseInt("dog") } catch { case _: Throwable => 0 } finally { println("always be printed") } //> always be printed //| result_try : Int = 0 }
object first { val code = 3 //> code : Int = 3 val result_match = code match { case 1 => "one" case 2 => "two" case _ => "others" } //> result_match : String = others }
package helloscala import java.io.PrintWriter object helloscala { def main(args: Array[String]) { val out = new PrintWriter("output.txt") for (i <- 1 to 5) out.println(i) out.close() } }
读取文本文件中的行 使用Scala.io.Source的getLines方法实现对文件中所有行的读取。
package helloscala import scala.io.Source object helloscala { def main(args: Array[String]) { val inputFile = Source.fromFile("output.txt") val lines = inputFile.getLines for (line <- lines) println(line) inputFile.close() } }
原文:https://www.cnblogs.com/anliven/p/10041570.html