Answers for "if null kotlin"

1

Kotlin when else do nothing

Kotlin has two existing possibilities to express a "do nothing" construct 
in when statements. Either Unit or an empty pair of braces. An empty block 
will just execute nothing. There's nothing else planned in that regard

To answer your question regarding "also, what is this actually doing?" 
for the empty block, looking at the bytecode and translating it into 
Java helps:

    val x = 33
    when(x)
    {
        1 -> {}
        2 -> Int
        3 -> Unit
        else -> Double
    }
    
Translates to
	int x = 33;
    switch(x) {
      case 1:
      case 3:
         break;
      case 2:
         IntCompanionObject var10000 = IntCompanionObject.INSTANCE;
         break;
      default:
         DoubleCompanionObject var1 = DoubleCompanionObject.INSTANCE;
    }
Posted by: Guest on November-13-2021
0

null check in kotlin

if(a != null)  {
//do something
}
Posted by: Guest on February-08-2021

Browse Popular Code Answers by Language