• Scala语言 学习记录(三)


    1、Scala中的空类型

      Unit、null、Nothing

    package com.demo3
    
    import com.demo1.Student
    
    /**
     * @author June
     * @date 2022/1/26 8:52
     */
    object Test3_1 {
      def main(args: Array[String]): Unit = {
        //空类型
        //  空值Unit
        def m1():Unit = {
          println("m1 被调用执行")
        }
    
        val a: Unit = m1()
        println("a:"+a)
    
        //空引用 Null
        //val n: Int = null
        // error 值类型不能接受空引用
        var student: Student = new Student("alice",33)
        student = null
        println(student)
    
    
        //Nothing
        def m2(n: Int): Int = {
          if(n == 0)
            throw new NullPointerException
          else
            return n
        }
    
        val b = m2(3)
        println(b)
      }
    }

    测试结果:

    变量值为空时,"()"括号输出的由来——源码查看:

      

    2、数值自动类型转换

    • 自动提升原则:有多种类型的数据混合运算时,系统首先自动将所有的数据换成精度大的那种数据类型
    • 把精度大的数值类型,赋值给精度小的数值类型时,就会报错,反之就会进行自动类型转换
    • byte short和char之间不会相互自动转换
    • byte short char 三者之间可以计算,在计算时首先转换为int类型
    • 首先转换为Int类型后,再进行变量之间的运算,最后强转为与赋值对象相同的数值类型
    package com.demo3
    
    /**
     * @author June
     * @date 2022/1/26 9:18
     */
    object Test3_2 {
      //自动类型转换
      def main(args: Array[String]): Unit = {
        //自动提升原则:有多种类型的数据混合运算时,系统首先自动将所有的数据换成精度大的那种数据类型,
        val a1: Byte = 10
        val b1: Long = 234233324L
        val result1: Long = (a1+b1)
        val result2: Int = (a1+b1).toInt    //强转
        println(result1+"    "+result2)
    
        //把精度大的数值类型,赋值给精度小的数值类型时,就会报错,反之就会进行自动类型转换
        val a2: Byte = 10
        val b2: Int = a2
        //val c3:Byte = b2        //错误赋值示例
    
        //byte short和char之间不会相互自动转换
        val a3: Byte = 10
        val b3: Char = 'b'
        //val c3: Byte = b3     //错误赋值示例
        val c3: Byte = b3.toByte
        println(c3)
    
        //byte short char 三者之间可以计算,在计算时首先转换为int类型
        val a4: Byte = 12
        val b4: Short = 23
        val c4: Char = 'd'
        //首先转换为Int类型后,再进行变量之间的运算,最后强转为与赋值对象相同的数值类型
        val result4: Short = (a4.toInt+b4.toInt).toShort
        val result5:Int = a4 + b4 + c4
        println(result4)
        println(result5)
    
      }
    }

    测试结果:

    与Java对比:

    package com.edu;
    
    /**
     * @author June
     * @date 2022/1/26 9:09
     */
    public class TestDataType {
        public static void main(String[] args) {
            byte b = 3;
            test(b);
            char c = 'a';
            test(c);
            int c2 = c;
            test(c2);
        }
    
        private static void test(byte b) {
            System.out.println("11111");
        }
    
        private static void test(short b) {
            System.out.println("22222");
        }
    
        private static void test(char b) {
            System.out.println("33333");
        }
    
        private static void test(int b) {
            System.out.println("44444");
        }
    }

    测试结果:

    3、强制类型转换

    • 将数据由高精度转换为低精度、需要用到强制类型转化
    • 强制转换符号只针对于最近的操作数有效,通常使用小括号提升优先级
    package com.demo3
    
    /**
     * @author June
     * @date 2022/1/26 10:28
     */
    object Test3_3 {
      //强制类型转换
      def main(args: Array[String]): Unit = {
        //将数据由高精度转换为低精度、需要用到强制类型转化
        val n1: Int = 2.5.toInt
        println(n1+"只是取整,即舍去小数部分,负数亦是如此")
        val n2: Int = -2.7.toInt
        println(n2+"只是取整,即舍去小数部分,负数亦是如此")
        //强制转换符号只针对于最近的操作数有效,通常使用小括号提升优先级
        val n3:Int = (2.3 + 3.3).toInt
        println(n3)
    
        //数值类型和String类型的转换
        //数值转 String
        val n: Int = 23
        val s: String = n + "3"
        println(s)
    
        //String 转数值
        val n4: Int = "32.33".toDouble.toInt
        val f1: Float = "22.2".toFloat
        val f2: Float = "2.44".toDouble.toFloat
        println(n4)
        println(f1)
        println(f2)
      }
    }

    测试结果:

      

    4、算术运算符

    package com.demo3
    
    /**
     * @author June
     * @date 2022/1/26 10:46
     */
    object Test3_4 {
      //算术运算符
      def main(args: Array[String]): Unit = {
        var n: Int = 256
        var b: Byte = n.toByte
        println(b)
        //算术运算符
        val result1: Int = 10/3
        println(result1)
    
        val result2: Double = 10/3
        println(result2)
    
        val result3: Double = 10.0/3
        println(result3.formatted("%.1f"))
    
        val result4: Int = 10%3
        println(result4)
    
        //比较运算符
        val str1: String = "hello"
        val str2: String = new String("hello")
        println(str1.equals(str2))
        println(str1.eq(str2))
        println(str1 == str2)
      }
    }

    测试结果:

      

    与Java对比:

    package com.edu;
    
    /**
     * @author June
     * @date 2022/1/26 11:14
     */
    public class TestOperator {
        public static void main(String[] args) {
            String s1 = "hello";
            String s2 = new String("hello");
    
            Boolean f = s1.equals(s2);
            System.out.println(f);
        }
    }

    测试结果:

      

  • 相关阅读:
    Anglarjs 工具方法
    AngularJs $scope 里面的$apply 方法和$watch方法
    CentOS 下tomcat安装
    Phonegap Android 项目使用Cordova
    Phonegap 原生控件(Android)与html混合
    Phonegap 通信原理
    Phonegap 开发环境搭建
    Phonegap 通知 Notification
    Phonegap 工作原理
    Angularjs MVC
  • 原文地址:https://www.cnblogs.com/lx06/p/15845985.html
Copyright © 2020-2023  润新知