Kotlin language

5 concepts · 15 practice questions

Null safety

Nullability is part of the type: String can never be null, String? can. Safe calls (?.), the Elvis operator (?:) and smart casts handle the null case; !! skips the check and throws if the value is null. Values from Java without annotations are platform types, which the compiler cannot check for you.

fun displayName(user: User?): String {
    val name = user?.profile?.name ?: return "Guest"
    return name.trim()
}

lateinit var adapter: FeedAdapter // set in onCreate, read after

Also covered

Data, sealed and value classes
Lambdas, inline and reified
Scope functions and delegation
Collections, sequences and generics

Sign in to read the rest and take the 15-question test.

Sign in to continue