Preface
This article mainly introduces the difference between ?. and !!! in Kotlin. It is shared for your reference and learning. I won’t say much below. Let’s take a look at the detailed introduction together.
1.?.
//kotlin:a?.foo()//Equivalent to java:if(a!=null){ a.foo();} 2.!!!
//kotlin:a!!.foo()//Equivalent to java: if(a!=null){ a.foo();}else{ throw new KotlinNullPointException();}Students who are precious time can not look at the following (` _ `)
3. Let’s talk about “!!”
It is worth mentioning that when we directly call a method or member variable of a nullable variable:
Call directly, report an error message
Android Studio will prompt that on a nullable variable, only "safe call (?.)) and "non-null assert call (!!.)) are allowed, and you can know that "!!" is equivalent to an assertion operation, that is:
//(Kotlin)a!!.foo()//Equal to (Kotlin):a!!a.foo()//Equivalent to (Java):assert a!=null;a.foo();//It is equivalent to (Java):if(a == null){ throw new NullPointException();}a.foo();Then there is the conclusion of the second point above.
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.