scala(一)

来源:互联网 发布:淘宝买115会员2017 编辑:程序博客网 时间:2024/06/08 19:35

1、变量和常量

scala> var a:String="hello"a: String = helloscala> var a="ji"a: String = jiscala> var a=1a: Int = 1scala> var a:Int=1--->可以定义变量的时候定义类型a: Int = 1scala> var a:Int=2a: Int = 2scala> var a="hello"a: String = helloscala> var a:String="world"a: String = worldscala> var a:Integer=22---->integer兼容int类型a: Integer = 22scala> val a = 20;---->常量,不可更改a: Int = 20scala> a=100<console>:12: error: reassignment to val       a=100        ^scala> var  a = 10;---->变量可更改a: Int = 10scala> a = 200a: Int = 200scala> 1 to 10res3: scala.collection.immutable.Range.Inclusive = Range 1 to 10scala> var a = 1 to 10--->定义一个数组类型a: scala.collection.immutable.Range.Inclusive = Range 1 to 10--->类型转换scala> 1.toto               toByte   toDegrees   toFloat       toInt    toOctalString   toShorttoBinaryString   toChar   toDouble    toHexString   toLong   toRadians       toStringscala> 1.tostring<console>:12: error: value tostring is not a member of Int       1.tostring         ^scala> 1.Tostring<console>:12: error: value Tostring is not a member of Int       1.Tostring         ^scala> 1.toStringres6: String = 1scala> 1.toDoubleres7: Double = 1.0scala> 1.toByteres8: Byte = 1

2、操作符重载

scala> 1+2res9: Int = 3scala> 1.+(2)--->通过.调用方法,括号里面相当于调用的加法方法的参数res10: Int = 3scala> 1.-(2)res11: Int = -1scala> 1.*(2)res12: Int = 2scala> 1.%(2)res13: Int = 1scala> 1.to(10)res14: scala.collection.immutable.Range.Inclusive = Range 1 to 10

3、scala没有++,–,只有+=

scala> var a = 100;a: Int = 100scala> a+=1;scala> ares25: Int = 101scala> a+=100;scala> ares27: Int = 201

4、导入包

scala> import scala.math._import scala.math._       --->下划线表示通配符scala> min(1,2)---->Scala函数没有对象,方法有对象,方法通过对象调用res29: Int = 1scala> 1.toString  --->对于没有参数的方法可以省略括号res30: String = 1scala> 1 toString  --->运算符的方式<console>:15: warning: postfix operator toString should be enabledby making the implicit value scala.language.postfixOps visible.This can be achieved by adding the import clause 'import scala.language.postfixOps'or by setting the compiler option -language:postfixOps.See the Scaladoc for value scala.language.postfixOps for a discussionwhy the feature should be explicitly enabled.       1 toString         ^res31: String = 1scala> 1.toString()    --->方法的形式res32: String = 1

5、apply方法

scala> "hello"(0)  --->隐含调用apply方法res35: Char = hscala> "hello".apply(0)---->等价于上面那个res36: Char = hscala> "hello".apply(1)res37: Char = escala> val a = "hello"a: String = helloscala> a(0)res38: Char = h

6、条件表达式

scala> var x = 3;x: Int = 3scala> var s = 0;s: Int = 0scala> if (x>1) s=1 else s =  -1;--->等价于上面的表达式scala> sres43: Int = 1scala> var b = if(x>1) 1 else -1; ---->scala的条件表达式都是有值的b: Int = 1scala> val c = if(x>1) 1 else "hello"c: Any = 1  ---->any类型是int类型和string类型的父类scala>

6、类型转换

scala> "1000". toIntres45: Int = 1000

7、unit类(赋值语句没有值,用unit类表示)

scala> val y = (s=1);y: Unit = ()scala> val y:Unit=();  ---->可以声明为unit类型,相当于voidy: Unit = ()

8、Scala没有switch语句

scala> if(x>0) 1  --->如何没有定义类型,会自动分配一个anyval类型res46: AnyVal = 1

9、进入粘贴模式(ctr+d结束粘贴模式)

scala> :paste// Entering paste mode (ctrl-D to finish)if(x>1){1}else -1// Exiting paste mode, now interpreting.res48: Int = 1scala>

10、Java—->.java–javac—->.class——>程序
打印输出

scala> print(1)1scala> println(2)2scala> printf("name is %s, age=%d","tong",12)name is tong, age=12scala>从终端读取scala> val name = readLine("请输入密码:");<console>:14: warning: method readLine in trait DeprecatedPredef is deprecated (since 2.11.0): use the method in `scala.io.StdIn`       val name = readLine("请输入密码:");                  ^请输入密码:name: String = 12323scala>
原创粉丝点击