当前位置:网站首页>变量和数据类型(04)完结
变量和数据类型(04)完结
2022-07-24 06:18:00 【Hyf 。】
Unit类型、Null类型和Nothing类型(重点)
基本说明
| 数据类型 | 描述 |
| Unit | 表示无值,和其他语言中void等同。用作不返回任何结果的方法的结果类型。Unit只是一个实例值,写成() |
| Null | null,Null类型只有一个实例值null |
| Nothing | Nothing类型在Scala的类型级最低端;它是任何其他类型的子类型。当一个函数,我们确定没有正常的返回值,可以用Nothing来指定返回类型,这样有一个好处,就是我们可以把返回的值(异常)赋给其他的函数或者变量(兼容性) |
案例实操
运行代码
package chapter02
import chapter01.Student
object Test07_DataType {
//5空类型
//5.1空值Unit
Unit 类型用来标识过程,也就是没有明确返回值的函数。
由此可见,Unit 类似于 Java 里的 void。Unit 只有一个实例——( ),这个实例也没有实
质意义
def m1():Unit = {
println("m1被调用执行")
}
val a = m1()
println("a:"+a)
//5.2空引用Null
Null 类只有一个实例对象,Null 类似于 Java 中的 null 引用。Null 可以赋值给任
意引用类型(AnyRef),但是不能赋值给值类型(AnyVal)
//val n:Int = null //错误的;值类型不能接收空引用
var student : Student = new Student("alice", 20)
student = null
println(student)
//5.3 Nothing
Nothing,可以作为没有正常返回值的方法的返回类型,非常直观的告诉你这个方
法不会正常返回,而且由于 Nothing 是其他任意类型的子类,他还能跟要求返回值的方法兼
容。
def m2(n:Int):Int ={
if (n == 0)
throw new NullPointerException
else
return n
}
val b= m2(2)
println("b:"+b)
}
}
运行结果

边栏推荐
猜你喜欢

tensorflow scatter_nd函数

Sparksql core usage, 220724,

C语言中extern,static, register,volatile 关键字的作用;保姆级教学!

Mac can't connect to local MySQL server through socket '/tmp/mysql Sock '(2) problem

tensorflow scatter_ Nd function

第二部分—C语言提高篇_2. 内存分区

《大厂面试》之JVM篇21问与答

STM32H750VBT6驱动程控增益放大模块PGA113——基于CubeMX的Hal库

10分钟就能写出来的——25~30K的国外企业招聘面试考题,这不是轻轻松松吗~
![[wechat applet] understand conditional rendering, list rendering and wxss template style](/img/97/cb78efcbcfe1a598da87751c482a98.png)
[wechat applet] understand conditional rendering, list rendering and wxss template style
随机推荐
It can be written in 10 minutes -- 25~30k foreign enterprise recruitment interview questions, isn't it easy~
Redis 持久化
PostgreSQL date handler usage
[learning notes] see the difference between a+++a and a+a++ from the compilation
MapReduce (I)
Lambda expressions sort list objects in multiple fields
tensorflow einsum函数
Never lose yourself!
别太在意别人的眼光,那会抹杀你的光彩
华为专家自述:如何成为优秀的工程师
Libevent multithreaded server + client source code
Don't care too much about what others think of you
sojson jsjiami.com. V6 crawler JS reverse
Accumulation of project problems
ROS starts non native nodes
【方向盘】超爱的IDEA提效神器Save Actions,卸载了
【学习笔记】url输入到页面展现中发生了什么?
UE4/5 无法打开文件“xxx.generated.h”(Cannot open file xxx.generated.h)的解决方法总结
不去和谁比较,只需做好自己
[USB voltmeter and ammeter] Based on stm32f103c8t6 for Arduino