Kotlin be empty type:???. !!

Nullable type:

Declare a variable in Kotlin, if the type is not added back? Can not be directly assigned to this variable is null, after the type plus? Becomes empty can type, and can be directly assigned to empty type null

Such as:

   var name: String = null//Error:Null can not be a value of a non-null type String
   var name1: String = null // empty type can be assigned to null?

?.  The normal call is the current face of variables! = Nuil, if it is null is null,

!! is when the variable is null, null pointer exception is thrown

Such as:

print (name1? .length) // When name1 is null, the output will be null

print (name1 !!. length) // When name1 is null is reported npe (Null type error) error

Elvis operator?:

Also known as null merge operator.

Function:
receiving two operands,
if the first operand is not null, the result of the operation is the first operand;
if the first operand is null, the result of the operation is the second operand.

Such as:

  var name: String? = null
  var nameLen: Int = name .length:?? 0 // will be calculated only when the result of the expression on the left is empty:? later expressions
  println(nameLen)

Guess you like

Origin blog.csdn.net/qq_35462323/article/details/89946811