DEV Community

Stone
Stone

Posted on

kotlin not-null judgment

?

When declaring an object instance,add ? after the type name indicates that the object can be null
When an object method is called, add ? after the instance name to indicate that null is returned once the instance is empty

?.

var length = str?.length;
when str is null,return null;when str is not null,return str.length.

?:

var length = str?.length?: -1
if str is null, return -1,else return str.lenght

!!

var length = str!!.length
if str is null,trow NullPointerException

Top comments (0)