Master Kotlin Programming with 50 free flashcards. Study using spaced repetition and focus mode for effective learning in Programming.
Null safety is a Kotlin feature that eliminates NullPointerException at compile time.
By default, variables cannot hold null. You must explicitly declare a type as nullable using ?, e.g. var name: String? = null.
Append a ? to the type declaration:var name: String? = null
Without the ?, the compiler will reject any attempt to assign null.
The safe call operator ?. allows you to access a property or call a method on a nullable object without risking a NullPointerException:val length = name?.length
If name is null, the expression returns null instead of throwing.
The Elvis operator ?: provides a default value when an expression is null:val len = name?.length ?: 0
If the left side is null, the right side is used.
The !! operator converts a nullable type to a non-null type and throws a KotlinNullPointerException if the value is null:val len = name!!.length
Use it only when you are absolutely certain the value is not null.
A data class automatically generates equals(), hashCode(), toString(), copy(), and componentN() functions:data class User(val name: String, val age: Int)
It is designed to hold immutable data.
A data class auto-generates:equals() and hashCode()toString() in the form User(name=John, age=30)copy() for creating modified copiescomponentN() functions for destructuring
copy() creates a new instance with the same values, allowing you to override specific properties:val user2 = user1.copy(age = 25)
This is useful for working with immutable data objects.
Extension functions let you add new functions to existing classes without modifying their source code:fun String.addExclamation(): String = this + "!"
Called as: "Hello".addExclamation() returns "Hello!".
No. Extension functions are resolved statically and do not actually modify the class. They cannot access private or protected members of the receiver class.
A sealed class restricts which classes can inherit from it. All subclasses must be defined in the same file (or same package in Kotlin 1.5+):sealed class Resultdata class Success(val data: String) : Result()data class Error(val msg: String) : Result()
Sealed classes allow the compiler to verify that a when expression covers all possible cases, eliminating the need for an else branch:when (result) {
is Success -> ...
is Error -> ...
}
Flashcards
Flip to reveal
Focus Mode
Spaced repetition
Multiple Choice
Test your knowledge
Type Answer
Active recall
Learn Mode
Multi-round mastery
Match Game
Memory challenge