Kotlin Programming

102 flashcards by MentisLibrary

Kotlin end to end: val and var, null safety and the elvis operator, extension and higher-order functions, data sealed and companion objects, the collection and sequence operators, coroutines and flows, generics and variance, and Java interop.

What is the difference between val and var?
val declares a read-only reference that can be assigned once; var can be reassigned. Both are statically typed, with the type usually inferred.
When must you write the type explicitly?
When there is no initializer, when the inferred type is not the one you want, and on every public function return type and parameter.
How does a string template work?
A $ inside a string inserts a variable, and ${...} inserts any expression. The compiler builds the concatenation for you.
Why is if different in Kotlin from Java?
It is an EXPRESSION that produces a value, so it replaces the ternary operator, which Kotlin does not have.
What can when match on?
Constant values, several values in one branch, ranges with in, types with is, and arbitrary boolean conditions when used with no subject.
How do you write a counting loop in Kotlin?
With a range: 1..10 counts up inclusively, until excludes the end, downTo counts down and step changes the increment.
What are Unit and Nothing?
Unit is the type of a function that returns no meaningful value, the equivalent of void, and it has exactly one instance. Nothing is the type of an expression that never returns, such as a throw.
Does Kotlin have primitive types?
Not in the source language: Int, Double and Boolean are classes. The compiler still emits JVM primitives where it can, so there is no boxing cost for a plain Int.
Why does assigning an Int to a Long fail to compile?
Kotlin performs no implicit widening conversions. You call the explicit conversion function instead.
What is the difference between == and ===?
== calls equals, so it compares content and handles nulls. === compares references, testing whether two names point at the same object.
What is a smart cast?
After is or a null check, the compiler treats the value as the narrowed type in that scope, so no explicit cast is needed.
What is the difference between as and as??
as is an unsafe cast that throws ClassCastException on failure. as? returns null instead, which pairs naturally with the elvis operator.
How is a Kotlin program's entry point declared?
A top-level fun main() function, optionally taking args: Array<String>. It needs no enclosing class.
How do you write a function whose body is one expression?
With = instead of braces. The return type is usually inferred, and there is no return keyword.
How does Kotlin's type system separate nullable from non-null?
A plain type cannot hold null. Appending ? makes it nullable, and the compiler then refuses any operation that would dereference it without a check.
What does the safe call ?. do?
Evaluates the member only when the receiver is non-null; otherwise the whole expression is null, and the rest of the chain is skipped.
What does the elvis operator ?: do?
Returns the left operand unless it is null, in which case it evaluates and returns the right. The right side may also throw or return.
What does !! do, and when is it justified?
It asserts a value is non-null, throwing NullPointerException if it is not. It is justified only when you can prove non-nullness the compiler cannot see.
What does ?.let { } express?
Run this block only if the value is non-null, with the value bound as it. It is the idiomatic replacement for a null-check block that also needs a smart cast.
What is a platform type?
A type coming from Java where nullability is unknown, written String! in diagnostics. Kotlin allows it to be used as either nullable or non-null and defers the check to run time.
What does lateinit allow, and what does it cost?
Declaring a non-null var that is assigned after construction, for dependency injection and test setup. Reading it before assignment throws UninitializedPropertyAccessException.
What is the difference between List<String?> and List<String>?
List<String?> is a non-null list that may contain null elements. List<String>? is a list reference that may itself be null but whose elements are all non-null.
How do you drop nulls from a collection?
filterNotNull() removes them and narrows the element type, and mapNotNull transforms and filters in one pass.
What do requireNotNull and checkNotNull do?
Both throw when their argument is null and return the non-null value otherwise. requireNotNull throws IllegalArgumentException for bad input, checkNotNull throws IllegalStateException for a bad state.
How do default parameter values change how you write functions?
They remove most overloads: one function declares defaults and callers supply only what differs, usually with named arguments.
What do named arguments buy you?
Call sites that read clearly and are safe against parameter reordering, and the ability to skip defaults in the middle of a list.
What is an extension function?
A function declared outside a type but callable with dot syntax on it, with the receiver available as this. It adds no state and modifies no class.
What is the shorthand for a single-parameter lambda?
The parameter can be omitted and referred to as it. Multi-parameter lambdas must name their parameters.
What is a higher-order function?
A function that takes a function as a parameter or returns one. The parameter's type is written as a function type such as (Int) -> String.
What is the trailing lambda convention?
When the LAST parameter is a function, the lambda can be moved outside the parentheses, and if it is the only argument the parentheses disappear.