简体   繁体   English

Kotlin - lateinit VS Any? = null

[英]Kotlin - lateinit VS Any? = null

In Kotlin there appears to be two method of declaring a variable inside an object that can be null and instantiated after the object is created. 在Kotlin中,似乎有两种方法在对象内声明一个变量,该变量可以为null并在创建对象后实例化。

var myObject : Any? = null

or 要么

var lateinit myObject : Any  

I am confused about why the lateinit keyword is needed if we can just make the var nullable and assign it later. 我很困惑为什么我们可以让var可以为空并在以后分配它时需要lateinit关键字。 What are the pros and cons of each method and in what situation should each one be used? 每种方法的优缺点是什么?在每种方法中应该使用哪种方法?

Here is how I see the difference according to my current knowledge in Kotlin. 以下是根据我目前在Kotlin的知识看到的差异。

First one: 第一:

var myObject1 : Any? = null

Here myObject1 is a property that is nullable. 这里myObject1是一个可以为空的属性。 That means you can assign null to it. 这意味着您可以为其分配null

Second one: 第二个:

lateinit var myObject2 : Any

Here myObject2 is a non-null property. 这里myObject2是一个非null属性。 That means you cannot assign null to it. 这意味着你不能为它分配null Usually if a property is non-null you have to initialize it at the declaration. 通常,如果属性为非null,则必须在声明处初始化它。 But adding the keyword lateinit allows you to postpone the initialization. 但添加关键字lateinit允许您推迟初始化。 If you try to access the lateinit property before initializing it then you get an exception. 如果在初始化之前尝试访问lateinit属性,则会出现异常。

In short the main difference is that myObject1 is a nullable and myObject2 is a non-null. 简而言之,主要区别在于myObject1是可空的,而myObject2是非空的。 The keyword lateinit provide you a convenience mechanism to allow a non-null property to be initialize at a later time rather than initializing it at the declaration. 关键字lateinit为您提供了一种便利机制,允许在以后初始化非null属性,而不是在声明中初始化它。

For more info check this . 有关详细信息,请检查

lateinit keyword is used on a field to avoid null checks when referencing the field inside of your object. lateinit关键字用于字段,以avoid在引用对象内部的字段时进行空检查。 The keyword is mainly used when you are using dependency injection to initialize the variable, or initializing the variable in the setup method of a unit test 当您使用依赖注入初始化变量或在单元测试的setup方法中初始化变量时,主要使用关键字

? is used on a field when the field will be initialized later in your program either by a setter or inside of a method of the object, this is to enforce you to check for null or use null safety( ?. ) when referencing the field 当字段将在程序中稍后通过setter或对象的方法内部初始化时,在字段上使用,这是为了强制您在引用字段时检查null或使用null安全性( ?.

If your property should not be null , but just isn't set after some point in the future, it is safer to declare it with a lateinit keyword. 如果你的属性不应该是null ,但是在将来某个点之后没有设置,那么使用lateinit关键字声明它是更安全的。 That guarantees that, if you access it before it is set, you get an exception explaining exactly that. 这保证了,如果你在设置它之前访问它,你会得到一个例外来解释它。

The traditional Java way is to throw a generic NullPointerException with no explanation about it. 传统的Java方法是抛出一个通用的NullPointerException而不解释它。 If you wrote the code yourself, you might have a clue, but if someone else catches the error, it won't be clear why that particular variable is null . 如果您自己编写代码,可能会有一些线索,但如果其他人捕获到错误,则不清楚为什么该特定变量为null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM