Kotlin 's Null safety

简介: Groovy's approach to null is the same as Java's – it requires you to handle it and guard against it.

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 StringString? 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.

相关文章
|
安全 Java 编译器
kotlin中学习笔记——null
指定一个变量可null是通过在它的类型后面加?号,如 val a: String? = null 复制代码 (注意这个Int也可为空是因为在kotlin中一切都是对象,包括在java中的基本类型) 一个可null类型,没有进行检查前不能使用,如下代码会编译出错 val a: String? = null a.subString(2)
308 0
|
Dart 安全
Flutter开发Cannot run with sound null safety报错
Flutter开发Cannot run with sound null safety报错
|
JavaScript Java Android开发
Android修行手册之Kotlin-【Null检查】、【类型检查】、【区间】篇
众所周知,人生是一个漫长的流程,不断克服困难,不断反思前进的过程。在这个过程中会产生很多对于人生的质疑和思考,于是我决定将自己的思考,经验和故事全部分享出来,以此寻找共鸣!!!
242 0
|
Dart 安全 编译器
dart系列之:安全看我,dart中的安全特性null safety
dart系列之:安全看我,dart中的安全特性null safety
|
Java Kotlin 编译器
Kotlin学习(二)—— 基本语法,函数,变量,字符串模板,条件表达式,null,类型检测,for,while,when,区间,集合
一.基本语法 Kotlin的很多概念跟JAVA是有类似的,所以我应该不会像我的JAVA之旅一样那么的详细,但是不用担心,你会看的很明白的,我也是根据官方的文档来学习的 我们在IDEA中创建一个项目Kotlin02 1.
1616 0
|
4月前
|
SQL 关系型数据库 MySQL
实时计算 Flink版产品使用合集之从MySQL同步数据到Doris时,历史数据时间字段显示为null,而增量数据部分的时间类型字段正常显示的原因是什么
实时计算Flink版作为一种强大的流处理和批处理统一的计算框架,广泛应用于各种需要实时数据处理和分析的场景。实时计算Flink版通常结合SQL接口、DataStreamAPI、以及与上下游数据源和存储系统的丰富连接器,提供了一套全面的解决方案,以应对各种实时计算需求。其低延迟、高吞吐、容错性强的特点,使其成为众多企业和组织实时数据处理首选的技术平台。以下是实时计算Flink版的一些典型使用合集。
|
1月前
|
SQL 关系型数据库 MySQL
在 MySQL 中使用 IS NULL
【8月更文挑战第12天】
583 0
在 MySQL 中使用 IS NULL
|
4月前
|
SQL 关系型数据库 MySQL
python在mysql中插入或者更新null空值
这段代码是Python操作MySQL数据库的示例。它执行SQL查询从表`a_kuakao_school`中选取`id`,`university_id`和`grade`,当`university_id`大于0时按升序排列。然后遍历结果,根据`row[4]`的值决定`grade`是否为`NULL`。若不为空,`grade`被格式化为字符串;否则,设为`NULL`。接着构造UPDATE语句更新`university`表中对应`id`的`grade`值,并提交事务。重要的是,字符串`NULL`不应加引号,否则更新会失败。
124 2
|
1月前
|
SQL 关系型数据库 MySQL
mysql不等于<>取特定值反向条件的时候字段有null值或空值读取不到数据
对于数据库开发的专业人士来说,理解NULL的特性并知道如何正确地在查询中处理它们是非常重要的。以上所介绍的技巧和实例可以帮助你更精准地执行数据库查询,并确保数据的完整性和准确性。在编写代码和设计数据库结构时,牢记这些细节将有助于你避免许多常见的错误,提高数据库应用的质量与性能。
35 0
|
2月前
|
SQL 存储 索引
MySQL设计规约问题之为什么应该把字段定义为NOT NULL并且提供默认值
MySQL设计规约问题之为什么应该把字段定义为NOT NULL并且提供默认值