Kotlin learning ---- ? ?: !! The use of operators (Kotlin fancy empty judgment)

Table of contents

First throw a conclusion:

? ?: how to use

? ?:in conclusion:

How to use !!

!!Summarize:


First throw a conclusion:

? Question mark modification, two ways to use

  • ? After the class name is modified to indicate that the object can be empty;
  • ? After the object is modified, it means that if the object is empty, the following code will not be executed

?: question mark colon modifier

  • ?: After the object, it means that if the object is empty, execute the code behind ?:

!! exclamation mark modifier

  • !! Put it behind the object, indicating that I will continue to execute even if the object is empty, and a null pointer exception may be thrown
  • //用于测试的对象返回器
    class ObjectReturner<T> {
        var r : Boolean = false
        var obj : T? = null
        constructor(r : Boolean , obj: T){
            this.r = r
            this.obj = obj
        }
    
        fun getObjMethod() : T?{
            return obj.takeIf { r }    //如果r为true返回obj,如果r为false返回null
        }
    }
    
    data class Student(val name : String , val age : Int)

    ? ?: how to use

  • var obj4: Student? = ObjectReturner(false , stu1).getObjMethod()
    println("obj4.name = ${obj4?.name}")
    obj4.run {
        println(this)
    } ?: println("怎么返回空了呢")

    The above method will never be executed to ?: The following statement is executed to the run method even if getObjMethod() returns empty , and finally prints null.

    println("obj4.name = ${obj4?.name}") If you directly access the properties of obj4 without adding a question mark here, it will not compile, because obj4 may be null.

    And using obj4.run can be compiled without adding a question mark (speechless)

    The reason for analyzing this depends on the code after kotlin is translated into java

    The decompilation result of obj4.run{println(this)}

  • Student obj4 = (Student)(new ObjectReturner(false, stu1)).getObjMethod();
    String var5 = "obj4.name = " + (obj4 != null ? obj4.getName() : null);    //看这里
    System.out.println(var5);    //这里是关键
    int var7 = false;
    System.out.println(obj4);

    The reason why obj4.run does not need to be added? It can be compiled and passed, because obj4.run does not use any methods or properties of obj4 after being translated into java. Because the run method is not a member method of obj4, it can be compiled normally and no exception will be reported during execution.

    The decompilation result of obj4?.run{println(this)}

  • Student obj4 = (Student)(new ObjectReturner(false, stu1)).getObjMethod();
    String var5 = "obj4.name = " + (obj4 != null ? obj4.getName() : null);//看这里
    System.out.println(var5);
    if (obj4 != null) {//看这里 是 ? 翻译后的java代码
       int var7 = false;
       System.out.println(obj4);
    } else {    //看这里 是 ?: 翻译后的java代码
       var5 = "怎么返回空了呢";
       System.out.println(var5);
    }

    when added? After calling the run method, the translated result will automatically add the judgment of obj4 != null.

    ? ?:in conclusion:

  • We added before calling the method or using the property? , the translated java code is actually an if judgment, and using ?:, the translated java code is actually an else

How to use !!

If an object is decorated with ?, it means that the object may be empty. At this time, if you use this object, you must add "?" or "!!" after the object, otherwise it will not compile

  • Add "?" As mentioned above, after java translation, if judgment will be automatically added, if it is empty, it will not be executed.
  • Adding "!!" means that you are very sure that the object is not empty, and force the compilation to pass. But if the final object is null, a null pointer exception will be thrown
  • var obj4: Student? = ObjectReturner(false , stu1).getObjMethod()
    println("obj4.name = ${obj4!!.name}")

    Translated java code:

  • Student obj4 = (Student)(new ObjectReturner(false, stu1)).getObjMethod();
    StringBuilder var11 = (new StringBuilder()).append("obj4.name = ");
    Intrinsics.checkNotNull(obj4);
    String var5 = var11.append(obj4.getName()).toString();

    !!Summarize:

    If obj4 is null, checkNotNull throws a null pointer exception.

    Adding "!!" means that you are very sure that the object is not empty, and force the compilation to pass. But if the final object is null, a null pointer exception will be thrown. !! Must be used with caution! ! ! !

Guess you like

Origin blog.csdn.net/mldxs/article/details/127157780