All Flashcards
(200 cards)
What’s an inline function?
- it instructs the compiler to insert the body of the function wherever the function gets used, which helps reduce function overhead and speeds up execution
- it works best for functions with lambda parameters, especially the function body is small
- it’s not recommended for large functions that are called in many places, because the code will be duplicated multiple times, increasing the program size
What’s const?
- the value is resolved at compile time
- it can only be used with
val
inside a companion object or at the top-level and it must be a primitive type like Int, Boolean or String - it removes the overhead of accessing the variable at runtime, which improves performance
- it can’t be used if the value is not static or it is a complex data type
What’s reified?
- the compiler erases the generic type, so its information is not available at runtime
- reified tells the compiler to retain the type information and insert it where it’s used
- after compilation, the generic type is replaced by its upper bound or Any
What’s the internal keyword?
it is used to restrict the visibility of a function, class, variable to the same module
What’s the open keyword?
- In Kotlin, classes and functions are final by default
- the open keyword is used to make a class extendable or a function overridable
What’s the lateinit keyword?
- it’s used when you want to initialize a variable later
- the variable must be non-nullable, can only be used with
var
and can’t be a primitive type like Int or Double
What’s by lazy keyword?
- it’s used when you want a variable to be initialized only when it is first accessed instead of at the time of declaration
- it improves performance since initialization happens only when needed
- the variable must work with
val
and once initialized, all future access will return the same value - it also ensures thread safety be default
What’s multidex?
- it’s used to overcome the limit on a large number of methods by allowing code to be split into multiple .dex files
- but it can slow down the app startup time
Is it possible to force GC?
No, it just makes a request to the system, but it doesn’t guarantee that it will happen
What’s @JvmStatic?
- it’s used to make functions and properties inside a companion object static in Java
- without @JvmStatic, functions and properties inside a companion object are not truly static. they can only be accessed through the companion object in Java
- with @JvmStatic, the compiler generates static functions and properties at the class level, making them directly accessible in Java
What’s @JvmField?
it’s used to instruct the compiler not to generate getter and setter methods for variables, making them directly accessible in Java and eliminating the function overhead
What’s @JvmOverloads?
- in Java, function parameters can’t have default values
- it’s used to instruct the compiler to generate overloads for the function with default parameters
When does the init block be called?
- frist, the primary constructor is called
- second, all init blocks are called in the order they are defined in the class
- finally, the secondary constructor is called
== and === in Kotlin?
- double equals checks if two objects have the same contents, which is similar to equals in Java
- triple equals checks if two objects refer to the same instance, which is similar to == in Java
What’s a Companion Object?
- it is tied to a class and defines static members within the class
- it is created when the class is loaded
What’s an extension function?
- it is used to add a new function to a class without modifying the class’s source code
- it is compiled as a static method and takes an instance of the class as a parameter
What’s noinline in Kotlin?
- it’s used to prevent a lambda parameter from being inlined in an inline function
- Problem:
- an inlined lambda can’t be passed to another noinline function
- an inlined lambda can’t be stored in a variable
What’s crossinline in Kotlin?
- it is used to prevent a non-local return inside a lambda in an inline function
- Problem:
- a local return causes the calling function to exit early
What’s a high-order function?
it’s a function that takes a lambda as a parameter or returns a lambda as the result
What’s infix in Kotlin?
- it’s used to define a function that is called in a more readable way without using dots and parentheses
- it is defined like an extension function but must have exactly one parameter which can’t have a default value
What’s inner class?
it’s used to define a nested class that can access all properties of the outer class, regardless of their visibilities
What’s inline class (value class)?
- it’s used to wrap a single value in a class without runtime overhead, providing type safety and improving performance by avoiding object allocations
- Feature
- it must have exactly one primary constructor parameter
- it can’t inherit from another class but it can implement interfaces
- it can declare properties, but they can’t have backing fields and can also declare functions
What’s sealed class?
- it a type of class that restricts the class hierarchy to a limited set of subclasses which must be declared in the same file
- it enables exhaustive checks in the when expression, ensuring all subclasses are handled, improving safety
What’s Kotlin DSL?
- it allows you to define configurations and tasks using Kotin, providing a more readable and type-safe way to write Gradle tasks and UI components in Compose
- Compare to using Groovy
- it is easier to read but may have a slower startup time because the code needs to be compiled to bytecode at build time