Kotlin Flashcards
Provide a brief history of kotlin focusing on the years 2011, 2016, 2017, 2019
● July 2011: Project Kotlin was first unveiled by JetBrains after a
year of development. It was unveiled as a new language for the JVM (key objective: drive IntelliJ IDEA sales).
*Android Studio is based on IntelliJ IDEA
● February 2016: Kotlin 1.0 was released.
● October 2017: Kotlin introduced as an alternative to Java in Android Studio 3
● May 2019: Google announced adoption of Kotlin as its preferred language for Android app developers.
● As of today, Kotlin is used by over 60% 0f professional Android developers.
What are Kotlin’s Key Advantages (7)
● Kotlin is an object-oriented tool, therefore having advantages pertaining to this paradigm
● Kotlin is compatible and interoperable with
other languages, and more so Java (full compatibility) and JS.
● Given its compatibility and interoperability, Kotlin is easily adoptable and easy to learn.
● Kotlin is less bulky with regards to code-writing
● Kotlin’s support from JetBrains and the community is tremendous
● Kotlin is well documented
● Kotlin is suitable for multi-platform development
given it’s compatibility with many other tools
What are the Android Kotlin’s Advantages
● Expressiveness and conciseness: less code writing.
● Safer code: Reduces crashing possibility of Android apps by 20%
● Interoperability: Kotlin is 100% interoberable with Java, the other key Android tool
● Structured concurrency: Kotlin coroutines make asynchronous code easy to work with, thus
simplifying background task management.
Describe and illustrate Package Definition and Imports
● These should be done at the top of a source file, just like in most other languages
package my.demo
import kotlin.text.*
Give the various variable declarations in Kotlin
– val a: Int = 1 // immediate assignment
– val b = 2 // Int
type is inferred
– val c: Int // Type is required when no initializer is provided
– c = 3 // deferred assignment
Explain the function of the main function in both a Kotlin application and an android function
Like many OOP tools, the main function is the entry point of a Kotlin application (this might not be so true for Android though, given the use of diverse methods to direct the Android app).
Define a simple main function without variables and one with a String of arguments accepted
//Simple main function without variables
fun main() {
println(“Hello World!”)
}
//Main function with a String of arguments accepted
fun main(args: Array<String>) {
println(args.contentToString())
}</String>
Describe and illustrate the use of the print function
There are options to either print with no line break, or to add a line break:
print(“Hello World”)
println(“Hello world!”)
Describe and illustrate the declaration of functions in Kotlin
● The return type, just like in variable declaration, comes after a function’s declaration.
● Same way some return types were inferred in variable declaration, is the same way we can have return types inferred in function declaration.
fun sum (a: Int, b: Int): Int {
return a + b
}
//An inferred return type on a function that is an expression
fun sum(a: Int, b: Int) = a + b
Describe and illustrate the declaration of classes in Kotlin
● The class keyword must be used to declare a class:
class Student
● Sometimes, properties can be listed inside the class’s declaration or body, with this providing the default constructor in the required format:
class Rectangle(var height: Double, var length: Double) {
var perimeter = (height + length) * 2
}
Describe and illustrate how inheritance is handled in kotlin
● Inheritance is declared by a colon (:)
● Classes are final by default, but to make a class inheritable, it should be marked by the key word open:
open class Shape
class Rectangle(var height: Double, var length: Double): Shape() {
var perimeter = (height + length) * 2
}
Describe Conditional Decision Making Statements in Kotlin
● Kotlin’s if, for and while statements are similar to Java’s.
● However, in Kotlin, if can also be used as an expression as below:
if (a > b) a else b
How is a range specified in Kotlin?
● The in operator can be used to check if a number is within a specified range:
val a = 10
val b = 9
if (a in 1..b+1) {
println(“fits in range”)
}
Describe and illustrate how Nullable Values and Null Checks are handled in Kotlin
● A reference must be explicitly marked as nullable when null value is possible.
● Nullable type names have ? at the end.
/* The following returns null if str does not hold an
integer. */
fun parseInt(str: String): Int? {
// …
}