当前位置:网站首页>Variables and data types (04) end
Variables and data types (04) end
2022-07-24 07:08:00 【Hyf 。】
Unit type 、Null The type and Nothing type ( a key )
Basic explanation
| data type | describe |
| Unit | It means no value , And other languages void equivalent . The result type used as a method that does not return any results .Unit Just an instance value , It's written in () |
| Null | null,Null Type has only one instance value null |
| Nothing | Nothing Type in the Scala Type level lowest end of ; It's a subtype of any other type . When a function , We are sure that there is no normal return value , It can be used Nothing To specify the return type , This has an advantage , That is, we can return the value ( abnormal ) Assign to other functions or variables ( Compatibility ) |
Case practice
Run code
package chapter02
import chapter01.Student
object Test07_DataType {
//5 Empty type
//5.1 Null value Unit
Unit Type is used to identify the process , That is, a function without an explicit return value .
thus it can be seen ,Unit Be similar to Java Inside void.Unit There is only one example ——( ), This example has no real
Qualitative meaning
def m1():Unit = {
println("m1 Called execution ")
}
val a = m1()
println("a:"+a)
//5.2 Null reference Null
Null Class has only one instance object ,Null Be similar to Java Medium null quote .Null Can be assigned to any
Meaning reference type (AnyRef), But cannot assign to value type (AnyVal)
//val n:Int = null // FALSE ; Value types cannot receive empty references
var student : Student = new Student("alice", 20)
student = null
println(student)
//5.3 Nothing
Nothing, Can be the return type of a method without a normal return value , Very intuitive to tell you this side
The method will not return normally , And because of Nothing Is a subclass of any other type , It can also be used with methods that require a return value
Rong .
def m2(n:Int):Int ={
if (n == 0)
throw new NullPointerException
else
return n
}
val b= m2(2)
println("b:"+b)
}
}
Running results

边栏推荐
- Ue4/5 cannot open the file "xxx.generated.h" (cannot open file xxx.generated.h) solution summary
- (note sorting is not completed) [graph theory: find the shortest path of single source]
- 上传图片base64
- /etc/rc. Local setting UI program startup and self startup
- 编译与调试(gcc,g++,gdb)
- B. Also Try Minecraft
- Use the root user to create a new user and set the password for
- C language from entry to soil (II)
- 【方向盘】IDEA的代码审查能力,来保证代码质量
- ROS starts non native nodes
猜你喜欢
随机推荐
[learning notes] see the difference between a+++a and a+a++ from the compilation
Empty cup mentality, start again
使用root用户为创建新用户并设置密码
8. Use the quadratic geometry technology to draw a small column on the screen.
ROS starts non native nodes
owasp top10 渗透测试
记账APP:小哈记账1——欢迎页的制作
File “manage.py“, line 14 ) from exc ^ SyntaxError: invalid syntax出错的原因及解决方法
[USB voltmeter and ammeter] Based on stm32f103c8t6 for Arduino
Talk about your thoughts about the future
记账APP:小哈记账2——注册页面的制作
Cmake notes
You can't satisfy everyone!
GIMP自定义截图
Sparksql core usage, 220724,
定制 or 通用,中国 SaaS 未来发展趋势是什么?
[wechat applet] understand conditional rendering, list rendering and wxss template style
树莓派换源
STM32H750VBT6驱动程控增益放大模块PGA113——基于CubeMX的Hal库
reflex








