当前位置:网站首页>Kotlin 's Null safety
Kotlin 's Null safety
2022-06-09 06:37:00 【51CTO】
Groovy's approach to null is the same as Java's – it requires you to handle it and guard against it. Its syntactic support for writing null-safe code is enhanced but the underlying problem remains.
Scala encourages and expects you to use its Option type and kind of pretends null doesn't exist. The problem is that any reference type in Scala (including a reference to Option) can be assigned a null value. Notwithstanding that you shouldn't do that, it can happen. For example when dealing with a Java API or when parsing JSON into a case class. It also means you have to write a wrapper for any legacy Java APIs you use to wrap nullable return types and parameters up in Option instances.
Kotlin treats a nullable reference to x as a different type to a non-nullable reference to x. This is indicated with a String? on the type name String — String? may be null, BUT String NOT. You must check for null before calling methods or accessing properties on anything potentially null. This is enforced by the compiler.
The key to understanding what's so great about this is that
Kotlin's String?
is an option type like
Scala's Option[String].
But it's also backwards compatible with every old Java API that might return null.
It's syntactically more convenient to deal with than Scala's Option or Java 8's Optional.
Kotlin's smart casts mean that once you've established that the reference is not null (e.g. in an if or when expression) that reference is automatically cast to the non-nullable type and you can then use it safely.
边栏推荐
- UML系列文章(23)高级行为---进程和线程
- 修改IDEA格式化单行注释 后增加空格
- Bladed software display incomplete solution
- Deep learning used mobile phone price forecast
- matlab如何向mat文件写入带标题的连续数据
- zgc gc消息类型及触发时机
- CodeBlocks always repeat the last result
- 戒烟日志_03 (day_07)
- RuntimeError: Dataset not found or corrupted. You can use download=True to download it
- BSP tailoring (6) Attachment III - rootfs menuconfig configuration description
猜你喜欢
![[raspberry pie 4B deep learning garbage classification] Chap.2 vgg16 & alexnet garbage dataset image classification, with a good project link [deep learning moves]](/img/e4/2c43244562d0137db5a331a662b53c.jpg)
[raspberry pie 4B deep learning garbage classification] Chap.2 vgg16 & alexnet garbage dataset image classification, with a good project link [deep learning moves]

clickhouse2分片2副本高可用集群搭建及chproxy代理配置使用

error: subprocess-exited-with-error(fasttext)

Intranet control: nodemcu (esp8266) and Arduino serial port communication

Chapter_ 02 how to scan and view images, query tables and time metrics in opencv

Chapter_ 01 mat: basic image container

若依 思维导图

BSP tailoring of Quanzhi platform (7) rootfs tailoring -- tailoring of user tools and Libraries & rootfs compression

指数移动加权平均

UML系列文章(21)高级行为---事件和信号
随机推荐
Chapter_02 如何扫描查看图像,查询表以及Opencv中的时间度量
Quanzhi v3s learning record -- use of esp8089
cms 和 g1的主要区别
Wechat applet page scrolls to the bottom
Bat renames files (folders) in batch
Ping: xxx: unknown name or service reason analysis
二叉树的递归套路
sparksql处理数据倾斜问题常见思路
Chapter_ 05 adding (fusing) two images using OpenCV
Add space after modifying idea format single line comment
无需剪辑软件,教你简单快速进行剪辑视频
YOLOv4解析 | 第二篇:用YOLOv4训练自己的数据集(超级详细完整版)
Deep learning used mobile phone price forecast
全志V3s学习记录(11)音频、视频使用总结
UML series articles (27) architecture modeling - Deployment
Quit smoking log_ 03 (day_07)
无缓存安装指令
新手,之前买了个理财产品,怎么看收益啊?
BSP tailoring (2) attachment 1 General setup configuration description
channel is not opened