Nullable.

In Kotlin the type system can difference between that can hold a null reference, or those that can’t hold a null reference for example if you define a variable like this

It will return a compilation error because this variable can’t hold a null, but If you define a variable in this way:

Like this You are declaring this variable as nullable string, but if you call a method or acces a property it isn’t guaranteed to not throw a Null Pointer Exception because the variable ‘b’ can be null.

Also you can check the null with conditions:

blank

Your second options is the safe call operator:

b?.length

This return the length of ‘b’ if is not null and null otherwise

Your third option is the Elvis Operator ‘?:’ is like the last option, this operators means the same, if hw?.length is null will return -1 or the value of hw.length instead. Since throw and return are expressions in Kotlin they can be used in the right side of Elvis operator:

blank

Your fourth option is !! operator, this one converts any value to a non-null type and throws a NPE if the value is null.

blank

Other option is the Safe Cast. If the object is not of the target type if the object is not of the target type you can return null if the cast wasn’t successful

blank

The last option is a filter of non-null in a collection of elements, using filterNotNull:

blank

The intList will contain (1,2,4)