Kotlin? And!!!!! The difference between

For those of you who are new to Kotlin and know that it manages Java NullPointerExceptions, in Kotlin, right? And!!!!! Nullpointerexceptions are both related, but what is the difference between them? Why are there so many “?” in other people’s projects? “And I read it with a question mark.

If you don’t understand, ask Baidu. Indeed, some people explain the difference between them, such as:

This is the input “Kotlin? And!!!!!” Baidu search to the first answer, indeed this said right. But I think for a new KT(Kotlin) novice, I’m afraid he needs to have Chinese level 8 to thoroughly understand the meaning of these two sentences.

First, two concepts:

“?” After the variable name, the system will not report its null pointer exception under any circumstances.

“!!!!!” If the object is null after the variable name, the system must raise an exception!

If a Java exception is thrown, the compiler will still let it throw an exception on KT. NullPointerException is the same.

So it can be seen from the context that! It doesn’t look any different from the previous Java code, and an exception will still be thrown in the place of the null. So most of the time, right? To detect NULL, not round!! Appearance. !!!!! This is only used when you need to make a non-null judgment on an object and throw an exception.

So let’s focus on that, okay? How exactly. When declaring an object, follow it after the class name to indicate that the class is allowed to be NULL. When an object is called, it follows the object, indicating that the program will ignore it if it is null. Such as the following code:

Var room: room? Var room: room? = Room() private fun checkRoom() {private fun checkRoom() {private fun checkRoom() { Log.d("TAG", "-->> room name = ${room? .roomname}")} // By default, room is added!! From now on, room cannot be null var room: Room = Room() private fun checkRoom() {private fun checkRoom() {Room = Room() private fun checkRoom() { D ("TAG", "-->> room name = ${room.roomname}")}Copy the code

So plus? Kotlin null Safety is a safe writing method, which embodies the characteristics of Kotlin null safety. The syntax of KT is very clever, and the definition parameters can be written as

val room: Room? Val Room: Room? Val Room: Room? Val room: room = room () val room = room () val room = room () val room = room () val room = room () val room = room () val room = room (Copy the code

But when you add a question mark and it’s done with NullPointerException forever? Let’s look at the next piece of code:

` val roomList: ArrayList<Room>? = null if (roomList? .size > 0) {log.d ("TAG", "-->> room number not 0")} 'Copy the code

Buta collection of facts called ‘Operator call buta pile to a dot-qualified call ‘roomList? .size.compareTo(0)’ which is not allowed on a nullable receiver ‘roomList?.size’.” When roomList is null, its size returns “null”, but “null” cannot be equal to the size of an int, so the compiler suggests that we write roomList? .size!! > 0.

Yes, it was added at the suggestion of the compiler!! When our program runs to this line of code, roomList is null and it must raise an exception. So do you have to cover it with an if(roomList! = null) this common Java statement to avoid exceptions?

Of course Kotlin doesn’t want this kind of verbose code in the program, so it provides object A, right? Object B expression, and cancellations conditional expressions in Java? Expression 1: The ternary expression of expression 2.

? : indicates that when object A is null, it returns the following object B.

`val roomList: ArrayList<Room>? = null val mySize= roomList? .size ? : 0Copy the code

`