package scala02object Scala07_typeCast {def main(args: Array[String]): Unit = {// TODO 隐式转换// 自动转换val b: Byte = 10var i: Int = b + 10val l: Long = b + 10 + 100Lval fl: Float = b + 10 + 100L + 10.5fval d: Double = b + 10 + 100L + 10.5f + 20.00println(d.getClass.getSimpleName)// 把精度大的数值赋值给精度小的数据类型 会报错// val d1:Float=3.5 ERRORval f: Double = 3.5f// byte short char 不会相互转换// val b1:Char=b error// byte short char可以计算 intval b1: Byte = 10val b2: Byte = 20val i1: Int = b1 + b2val s: Short = 100val i2: Int = b1 + 2// int和int相加val i3: Int = 1000val i4: Int = 1000val i5 :Int =i3+i4val i6: Int = b + s + 'a'println(i6)// TODO 强制类型转换 // 类型从大转小val d1:Double=10.5val l1: Long = d1.toLongprintln(l1) // 如果想完成四舍五入效果(d1+0.5).val l2:Long = (d1+0.5).toLongprintln(l2) // TODO 数值类型和STRING类型转换val str:String="100"val int: Int = str.toIntprintln(int.getClass.getSimpleName)val int1:Int=100val string: String = int1.toStringprintln(string.getClass.getSimpleName) // 不能跨级转换 // val int2:Int="100.5".toInt error val int2:Int="100.5".toDouble.toIntprintln(int2)}}
package scala03object scala01_opera {def main(args: Array[String]): Unit = { // == 和 equals 作用一样 比较具体值大小val s1: String = new String("aaa")val s2:String=new String("aaa")println(s1.equals(s2))println(s1 == s2) // true true // 比较地址值println(s1.eq(s2))// 运算符本质就是方法1+1val i: Int = 1.+(1)println(i) // 调方法的时候.可以省略 // 当方法中的参数只有一个的时候 括号也可以省略}}
package scala04object scala01_for_if_else {def main(args: Array[String]): Unit = {// 三元运算符val age: Int = 10val str1: String = if (age > 18) "成年" else "未成年"}}
package scala04import scala.collection.parallel.immutableobject scala02_for {def main(args: Array[String]): Unit = {// for// scala中的循环变量i 不能声明到for之外for (i <- 0 to 5) {println(i)}println("====================================")// 不包含5for (i <- 0 until (5)) {println(i)}// 循环本质// f范围内的元素放入一个集合中 ,遍历集合val inclusive: Range.Inclusive = 0.to(5)for (elem <- inclusive) {println(elem)}// for 循环的补充语法// 循环守卫for (i <- 0 to 5) {if (i < 3) {println(i)}}println("====================================")for (i <- 0 to 5 if i < 3) {println(i)}println("====================================")// 循环步长for (i <- 0 to 10 by 2) {println(i)}for (i <- 0.0 to 10 by 0.5) {println(i)}println("====================================")// 嵌套循环for (i <- 0 to 3){for (j<-0to 4){println(s"$i*$j=${i*j}\t")}println()}for (i<-0 to 3;j<-0 to 4){println(s"$i*$j=${i*j}\t")if (i==4){println()}}println("====================================") // 引入变量for (i<-0 to 5 ;a=1+i){println(a)}// 循环返回值 yield 关键字进行标识 val unit: Unit = for (i <- 0 to 5) {println(i)//}//val ints : immutable.IndexedSeq[Int]=for } // 倒序打印 for (i <-0 to 5 reverse){println(i) }println("====================================")for (i <-0 to 5 by -1){println(i)}}}
package scala04import scala.collection.parallel.immutableobject scala02_for {def main(args: Array[String]): Unit = {// for// scala中的循环变量i 不能声明到for之外for (i <- 0 to 5) {println(i)}println("====================================")// 不包含5for (i <- 0 until (5)) {println(i)}// 循环本质// f范围内的元素放入一个集合中 ,遍历集合val inclusive: Range.Inclusive = 0.to(5)for (elem <- inclusive) {println(elem)}// for 循环的补充语法// 循环守卫for (i <- 0 to 5) {if (i < 3) {println(i)}}println("====================================")for (i <- 0 to 5 if i < 3) {println(i)}println("====================================")// 循环步长for (i <- 0 to 10 by 2) {println(i)}for (i <- 0.0 to 10 by 0.5) {println(i)}println("====================================")// 嵌套循环for (i <- 0 to 3){for (j<-0to 4){println(s"$i*$j=${i*j}\t")}println()}for (i<-0 to 3;j<-0 to 4){println(s"$i*$j=${i*j}\t")if (i==4){println()}}println("====================================") // 引入变量for (i<-0 to 5 ;a=1+i){println(a)}// 循环返回值 yield 关键字进行标识 val unit: Unit = for (i <- 0 to 5) {println(i)//}//val ints : immutable.IndexedSeq[Int]=for } // 倒序打印 for (i <-0 to 5 reverse){println(i) }println("====================================")for (i <-0 to 5 by -1){println(i)}}}
package scala04import scala.util.control.Breaks import scala.util.control.Breaks.{break, breakable}object scala04_break {def main(args: Array[String]): Unit = {//scala中使用breaks体系进行循环终止Breaks.breakable(for (i <- 0 to 10) {if (i == 5) {Breaks.break()}println(i)}) // 简化写法breakable(for (i <-0 to 10){if(i==5){break()}println(i)})println("===================================================") // scala 中循环终端原理就是使用异常处理体系try {for (i <- 0 to 10) {if (i == 5) {// 通过抛一场循环打断throw new RuntimeException}println(i)}}catch {case e:RuntimeException => println ("循环终端")}finally {println("关闭资源....")}}}