当前位置:网站首页>变量和数据类型(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)
}
}
运行结果

边栏推荐
- reflex
- Don't compare with anyone, just be yourself
- [USB voltmeter and ammeter] Based on stm32f103c8t6 for Arduino
- Redis distributed cache learning notes
- 上传图片base64
- Sealos packages and deploys kubesphere container platform
- 反射
- Libc.so.6/glibc cross compilation
- Penetration learning - SQL injection - shooting range - installation and bypass experiment of safety dog (it will be updated later)
- 【学习笔记】网页出现白屏可能的原因与优化方法
猜你喜欢
![[waveform / signal generator] Based on stc1524k32s4 for C on Keil](/img/62/8bf96bf7617cc9b2682b5a32711878.png)
[waveform / signal generator] Based on stc1524k32s4 for C on Keil

【学习笔记】网页出现白屏可能的原因与优化方法

tensorflow einsum函数

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

GE口:SGMII模式和serdes模式

Three level classification / menu query tree structure

在线问题反馈模块实战(十二):实现图片删除功能

Redis fragment cluster

"Big factory interview" JVM Chapter 21 questions and answers

Jinan renshe has signed 1w+ electronic labor contract, which greatly helps HR digitalization
随机推荐
Redis special data type hyperloglog
Accumulation of project problems
tensorflow einsum函数
[C language] operator details (in-depth understanding + sorting and classification)
项目问题积累
【LeetCode】444. 序列重建
Redis special data type bitmap
Can you increase muscle without exercise??? Just get an injection of hibernating black bear serum
Vs2019 configuration running open3d example
Camera Hal OEM模块 ---- cmr_grab.c
Redis basic type - hash
GIMP自定义截图
Redis 分片集群
STM32基于hal库的adc以DMA的多通道采样以及所遇问题解决
[learning notes] Web page rendering process
不去和谁比较,只需做好自己
The function of extern, static, register, volatile keywords in C language; Nanny level teaching!
SPI——发送16位和8位数据
PyTorch 深度学习实践 第10讲/作业(Basic CNN)
你是谁由你自己决定!