SoFunction
Updated on 2025-04-10

How to use Kotlin hollow judgment and question mark and exclamation mark identifier

If someone makes statistics on the cause of the program's crash, then the crash caused by the empty object but accessing a certain property of the object may be the number one reason for the program's crash.

For example, when we use strings and the variable string is empty, we access the length of the string variable, which results in the program crash.

Later we have experience. When accessing the properties of an object, we have to make a judgment on whether the object is empty first, so our program becomes like this:

if  (str!=null  &&  ()>0  &&  ().length()>0)  {}

This seems very rigorous, but it is actually very verbose. Many codes are unnecessary, and in order to improve the stability of the program, the program uses this judgment everywhere.

In the final analysis, all compilers have no way to check whether the runtime object is empty.

Nullable variable

Although the program crash caused by accessing the properties of empty variables is largely due to programmers' problems, as a compiler or programming specification, what are the areas that can be improved?

KotlinIn terms of the properties of a variable, the variable is divided into nullable variables and non-nullable variables.

The variables we declare are non-null variables. We need to assign values ​​from the beginning of non-null variables, otherwise the following error will occur:

Variables have no assignment:

        var str:String
        var len = 

Error prompt:

Variable 'str' must be initialized

If an nullable variable is declared, you can not assign a value at the beginning. When an empty object accesses some properties, an error will be reported.

        var strn:String? =null
        println()

Then the following error will be reported:

Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type String?

KotlinIn the empty judgment, this new feature has been made for us, but it still depends on the scientific use of programmers and programmers to best ensure the stability of the program.

!! operator

KotlinI helped us with some empty objects, for some controllable objects,KotlinWhen accessing certain properties, if the object is empty, even if some properties are accessed, it will not crash directly, but will return an empty object.

If we are not sure if the object has a value, we can access the property like this:

        var strn:String? =null
        var len = strn?.length

You can understand this?, is our uncertainty about this variable.

If you are very confident in this variable, you can also add two!! and emphasize that you are very sure that it is not empty.

        var strn:String? =null
        var len = strn?.length
        strn = "1231"
        println(strn!!.length)

If you have confidence, you can useletKeywords.

        var myText:String? = null
        myText = "hello let"
        myText?.let {
            var tv_text: TextView = findViewById(.tv_test) as TextView
            tv_text.text = myText
        }

whenmyText fornull, will not enterlet The function inside is equivalent to the action that helps us determine whether it is empty. Is it much simpler to write? If you are not sure in the program, please refer to itlet It will reduce many program crash problems.

summary

Most of the reasons for the crash in the program are that we access an empty variable and the program exits at this time. Of course, this is caused by the programmer's accidental care, but for languages, we can help us check first, or write the smart points of the compiler, so that programmers can make these mistakes frequently.KotlinI have done a lot for us, and if we encounter the operators of ?? and !! again, we will understand it easily.

This is the article about Kotlin hollow judgment and how to use question marks and exclamation mark identifiers. For more related Kotlin empty judgment content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!