Categories
Top 40 Kotlin Interview Questions and Answers
Kotlin is immensely practical. It addresses the problems that developers have, and not some guys in the academia. So, it has type inference, it has amazing type safety, good collection library, and concurrency library to top it. And it's now official - a lot of organisations are migrating their backend applications to Kotlin, and this trend is not likely to end soon.
1. How to create singleton in Kotlin?
Just use
object
object SomeSingleton
The above Kotlin object will be compiled to the following equivalent Java code:
public final class SomeSingleton {
public static final SomeSingleton INSTANCE;
private SomeSingleton() {
INSTANCE = (SomeSingleton)this;
System.out.println("init complete");
}
static {
new SomeSingleton();
}
}
2. How to initialize an array in Kotlin with values?
Problem
In Java an array can be initialized such as: int numbers[] = new int[] {10, 20, 30, 40, 50}
How does Kotlin's array initialization look like?
val numbers: IntArray = intArrayOf(10, 20, 30, 40, 50)
3. What is the difference between var and val in Kotlin?
var
is like general variable and it's known as a mutable variable in kotlin and can be assigned multiple times.val
is like Final variable and it's known as immutable in Kotlin and can be initialized only single time.val | var | |
Reference type | Immutable(once initialized can't be reassigned) | Mutable(can able to change value) |
Example | val n = 20 | var n = 20 |
In Java | final int n = 20; | int n = 20; |
4. What is basic difference between fold and reduce in Kotlin? When to use which?
fold
takes an initial value, and the first invocation of the lambda you pass to it will receive that initial value and the first element of the collection as parameters.listOf(1, 2, 3).fold(0) { sum, element -> sum + element }
The first call to the lambda will be with parameters 0 and 1.
Having the ability to pass in an initial value is useful if you have to provide some sort of default value or parameter for your operation.
Having the ability to pass in an initial value is useful if you have to provide some sort of default value or parameter for your operation.
reduce
doesn't take an initial value, but instead starts with the first element of the collection as the accumulator (called sum in the following example)listOf(1, 2, 3).reduce { sum, element -> sum + element }
5. What is a data class in Kotlin?
We frequently create classes whose main purpose is to hold data. In Kotlin, this is called a data class and is marked as data:
data class User(val name: String, val age: Int)
To ensure consistency and meaningful behavior of the generated code, data classes have to fulfill the following requirements:
The primary constructor needs to have at least one parameter;
All primary constructor parameters need to be marked as val or var;
Data classes cannot be abstract, open, sealed or inner;
The primary constructor needs to have at least one parameter;
All primary constructor parameters need to be marked as val or var;
Data classes cannot be abstract, open, sealed or inner;
6. How is it recommended to create constants in Kotlin?
In Kotlin, if you want to create the local constants which are supposed to be used with in the class then you can create it like below:
val MY_CONSTANT_1 = "Constants1"
const val MY_CONSTANT_2 = "Constants2"
Like val, variables defined with the const keyword are immutable. The difference here is that const is used for variables that are known at compile-time.
Also avoid using companion objects. Behind the hood, getter and setter instance methods are created for the fields to be accessible. Calling instance methods is technically more expensive than calling static methods. Instead define the constants in object:
Also avoid using companion objects. Behind the hood, getter and setter instance methods are created for the fields to be accessible. Calling instance methods is technically more expensive than calling static methods. Instead define the constants in object:
object DbConstants {
const val TABLE_USER_ATTRIBUTE_EMPID = "_id"
const val TABLE_USER_ATTRIBUTE_DATA = "data"
}
7. Explain what is wrong with that code?
Problem
Why is this code wrong?class Student (var name: String) {
init() {
println("Student has got a name as $name")
}
constructor(sectionName: String, var id: Int) this(sectionName) {
}
}
Answer
The property of the class can’t be declared inside the secondary constructor.. This will give an error because here we are declaring a property id of the class in the secondary constructor, which is not allowed.If you want to use some property inside the secondary constructor, then declare the property inside the class and use it in the secondary constructor:
class Student (var name: String) {
var id: Int = -1
init() {
println("Student has got a name as $name")
}
constructor(secname: String, id: Int) this(secname) {
this.id = id
}
}
8. Explain the null safety in Kotlin
Answer
Kotlin's type system is aimed at eliminating the danger of null references from code, also known as the The Billion Dollar Mistake.One of the most common pitfalls in many programming languages, including Java, is that accessing a member of a null reference will result in a null reference exception. In Java this would be the equivalent of a NullPointerException or NPE for short.
In Kotlin, the type system distinguishes between references that can hold null (nullable references) and those that can not (non-null references). For example, a regular variable of type String can not hold null:
var a: String = "abc"
a = null // compilation error
To allow nulls, we can declare a variable as nullable string, written String?:
var b: String? = "abc"
b = null // ok
print(b)
9. Explain advantages of when vs switch in Kotlin
Answer
In Java we use switch but in Kotlin, that switch gets converted to when. When has a better design. It is more concise and powerful than a traditional switch. when can be used either as an expression or as a statement.Some examples of when usage:
Two or more choices
when(number) {
1 -> println("One")
2, 3 -> println("Two or Three")
4 -> println("Four")
else -> println("Number is not between 1 and 4")
}
"when" without arguments
when {
number < 1 -> print("Number is less than 1")
number > 1 -> print("Number is greater than 1")
}
Any type passed in "when"
fun describe(obj: Any): String =
when (obj) {
1 -> "One"
"Hello" -> "Greeting"
is Long -> "Long"
!is String -> "Not a string"
else -> "Unknown"
}
Smart casting
when (x) {
is Int -> print("X is integer")
is String -> print("X is string")
}
10. Where should I use var and where val?
Answer
Use var where value is changing frequently. For example while getting location of android device:var integerVariable : Int? = null
Use val where there is no change in value in whole class. For example you want set textview or button's text programmatically.
val stringVariables : String = "Button's Constant or final Text"
11. Rewrite this code in Kotlin
Problem
Can you rewrite this Java code in Kotlin?public class Singleton {
private static Singleton instance = null;
private Singleton(){
}
private synchronized static void createInstance() {
if (instance == null) {
instance = new Singleton();
}
}
public static Singleton getInstance() {
if (instance == null) createInstance();
return instance;
}
Answer
Using Kotlin:object Singleton
12. How would you refactor this code using apply?
Problem
Consider:class Message(message: String, signature: String) {
val body = MessageBody()
init {
body.text = message + "\n" + signature
}
}
Answer
You can write:class Message(message: String, signature: String) {
val body = MessageBody().apply {
text = message + "\n" + signature
}
}
13. May you use IntArray and an Array<Int> is in Kotlin interchangeably?
Array is an Integer[] under the hood, while IntArray is an int[].
This means that when you put an Int in an Array, it will always be boxed (specifically, with an Integer.valueOf() call). In the case of IntArray, no boxing will occur, because it translates to a Java primitive array.
This means that when you put an Int in an Array, it will always be boxed (specifically, with an Integer.valueOf() call). In the case of IntArray, no boxing will occur, because it translates to a Java primitive array.
14. What are coroutines in Kotlin?
Unlike many other languages with similar capabilities, async and await are not keywords in Kotlin and are not even part of its standard library.
kotlinx.coroutines is a rich library for coroutines developed by JetBrains. It contains a number of high-level coroutine-enabled primitives, including launch, async and others. Kotlin Coroutines give you an API to write your asynchronous code sequentially.
The documentation says Kotlin Coroutines are like lightweight threads. They are lightweight because creating coroutines doesn’t allocate new threads. Instead, they use predefined thread pools, and smart scheduling. Scheduling is the process of determining which piece of work you will execute next.
Additionally, coroutines can be suspended and resumed mid-execution. This means you can have a long-running task, which you can execute little-by-little. You can pause it any number of times, and resume it when you’re ready again.
kotlinx.coroutines is a rich library for coroutines developed by JetBrains. It contains a number of high-level coroutine-enabled primitives, including launch, async and others. Kotlin Coroutines give you an API to write your asynchronous code sequentially.
The documentation says Kotlin Coroutines are like lightweight threads. They are lightweight because creating coroutines doesn’t allocate new threads. Instead, they use predefined thread pools, and smart scheduling. Scheduling is the process of determining which piece of work you will execute next.
Additionally, coroutines can be suspended and resumed mid-execution. This means you can have a long-running task, which you can execute little-by-little. You can pause it any number of times, and resume it when you’re ready again.
15. What are some disadvantages of Kotlin?
Some think that Kotlin is a mess of extra syntax and keywords. Here are a few keywords which have non-obvious meanings: internal, crossinline, expect, reified, sealed, inner, open. Java has none of these. Kotlin is also amusingly inconsistent in its keywords: a function is is declared with ‘fun’, but an interface is declared with ‘interface’ (not ‘inter’?). Kotlin also doesn’t have checked exceptions. Checked exceptions have become unfashionable, yet many (including me) find them a powerful way to ensure that your code is robust. Finally, Kotlin hides a lot of what goes on. In Java, you can trace through almost every step of program logic. This can be vital for hunting down bugs. In Kotlin, if you define a data class, then getters, setters, equality testing, to string, and hash code are added for you invisibly. This can be a bad idea.
Also according docs, what Java has that Kotlin does not:
1) Checked exceptions
2) Primitive types that are not classes
3) Static members
4) Non-private fields
5) Wildcard-types
6) Ternary-operator a ? b : c
Also according docs, what Java has that Kotlin does not:
1) Checked exceptions
2) Primitive types that are not classes
3) Static members
4) Non-private fields
5) Wildcard-types
6) Ternary-operator a ? b : c
16. What is lateinit in Kotlin and when would you use it?
lateinit means late initialization. If you do not want to initialize a variable in the constructor instead you want to initialize it later on and if you can guarantee the initialization before using it, then declare that variable with lateinit keyword. It will not allocate memory until initialized. You cannot use lateinit for primitive type properties like Int, Long etc.
lateinit var test: String
fun doSomething() {
test = "Some value"
println("Length of string is "+test.length)
test = "change value"
}
There are a handful of use cases where this is extremely helpful, for example:
Spring Boot annotations (eg. @Autowired).
Android
variables that get initialized in lifecycle methods;Using Dagger for DI:
injected class variables are initialized outside and independently from the constructor;Setup for unit tests:
test environment variables are initialized in a @Before - annotated method;Spring Boot annotations (eg. @Autowired).
17. What is a purpose of Companion Objects in Kotlin?
Unlike Java or C#, Kotlin doesn’t have static members or member functions. If you need to write a function that can be called without having a class instance but needs access to the internals of a class, you can write it as a member of a companion object declaration inside that class.
class EventManager {
companion object FirebaseManager {
}
}
val firebaseManager = EventManager.FirebaseManager
The companion object is a singleton. The companion object is a proper object on its own, and can have its own supertypes - and you can assign it to a variable and pass it around. If you're integrating with Java code and need a true static member, you can annotate a member inside a companion object with @JvmStatic.
class EventManager {
companion object FirebaseManager {
}
}
val firebaseManager = EventManager.FirebaseManager
The companion object is a singleton. The companion object is a proper object on its own, and can have its own supertypes - and you can assign it to a variable and pass it around. If you're integrating with Java code and need a true static member, you can annotate a member inside a companion object with @JvmStatic.
18. What is the Kotlin double-bang !! operator?
The not-null assertion operator !! converts any value to a non-null type and throws a KotlinNullPointerException exception if the value is null.
fun main(args: Array<String>) {
var email: String?
email = null
println(email!!)
}
19. What is the difference between suspending vs. blocking?
A blocking call to a function means that a call to any other function, from the same thread, will halt the parent’s execution. Following up, this means that if you make a blocking call on the main thread’s execution, you effectively freeze the UI. Until that blocking calls finishes, the user will see a static screen, which is not a good thing.
Suspending doesn’t necessarily block your parent function’s execution. If you call a suspending function in some thread, you can easily push that function to a different thread. In case it is a heavy operation, it won’t block the main thread. If the suspending function has to suspend, it will simply pause its execution. This way you free up its thread for other work. Once it’s done suspending, it will get the next free thread from the pool, to finish its work.
Suspending doesn’t necessarily block your parent function’s execution. If you call a suspending function in some thread, you can easily push that function to a different thread. In case it is a heavy operation, it won’t block the main thread. If the suspending function has to suspend, it will simply pause its execution. This way you free up its thread for other work. Once it’s done suspending, it will get the next free thread from the pool, to finish its work.
20. What is the difference between List and Array types?
The major difference from usage side is that Arrays have a fixed size while (Mutable)Listcan adjust their size dynamically. Moreover Array is mutable whereas List is not.
Furthermore kotlin.collections.List is an interface implemented among others by java.util.ArrayList. It's also extended by kotlin.collections.MutableListto be used when a collections that allows for item modification is needed.
On the jvm level Array is represented by arrays. List on the other hand is represented by java.util.List since there are no immutable collections equivalents available in Java.
Furthermore kotlin.collections.List is an interface implemented among others by java.util.ArrayList. It's also extended by kotlin.collections.MutableListto be used when a collections that allows for item modification is needed.
On the jvm level Array is represented by arrays. List on the other hand is represented by java.util.List since there are no immutable collections equivalents available in Java.
21. What is the difference between const and val?
consts are compile time constants. Meaning that their value has to be assigned during compile time, unlike vals, where it can be done at runtime.
This means, that consts can never be assigned to a function or any class constructor, but only to a String or primitive.
This means, that consts can never be assigned to a function or any class constructor, but only to a String or primitive.
22. What is the difference between open and public in Kotlin?
The open keyword means “open for extension“. The open annotation on a class is the opposite of Java's final: it allows others to inherit from this class.
If you do not specify any visibility modifier, public is used by default, which means that your declarations will be visible everywhere. public is the default if nothing else is specified explicitly.
If you do not specify any visibility modifier, public is used by default, which means that your declarations will be visible everywhere. public is the default if nothing else is specified explicitly.
23. What is the equivalent of Java static methods in Kotlin?
Place the function in the companion object.
class Foo {
public static int a() { return 1; }
}
class Foo {
companion object {
fun a() : Int = 1
}
}
// to run
Foo.a();
Another way is to solve most of the needs for static functions with package-level functions. They are simply declared outside a class in a source code file. The package of a file can be specified at the beginning of a file with the package keyword. Under the hood these "top-level" or "package" functions are actually compiled into their own class. In the above example, the compiler would create a class FooPackage with all of the top-level properties and functions, and route all of your references to them appropriately.
Consider
Consider
package foo
fun bar() = {}
import foo.bar
24. What is the purpose of Unit-returning in functions? Why is VALUE there? What is this VALUE?
Explain what is the purpose of Unit-returning in functions? Why is VALUE there? What is this VALUE?
Problem
fun printHello(name : String?) : Unit {
if (name != null)
print("Hello, $name!")
else
print("Hi there!")
// We don't need to write 'return Unit.VALUE' or 'return', although we could
}
Answer
The purpose is the same as C's or Java's void. Only Unit is a proper type, so it can be passed as a generic argument etc.Why we don't call it "Void": because the word "void" means "nothing", and there's another type, Nothing, that means just "no value at all", i.e. the computation did not complete normally (looped forever or threw an exception). We could not afford the clash of meanings.
Why Unit has a value (i.e. is not the same as Nothing): because generic code can work smoothly then. If you pass Unit for a generic parameter T, the code written for any T will expect an object, and there must be an object, the sole value of Unit.
UNIT actually contains valuable information, it basically just means "DONE". It just returns the information to the caller, that the method has been finished.
25. What will be result of the following code execution?
Problem
What will be the output?val aVar by lazy {
println("I am computing this value")
"Hola"
}
fun main(args: Array<String>) {
println(aVar)
println(aVar)
}
I am computing this value
Hola
Hola
26. When would you use Elvis operator in Kotlin?
The Elvis operator is part of many programming languages, e.g. Kotlin but also Groovy or C#. The Elvis operator is the ternary operator with its second operand omitted.
x ?: y // yields `x` if `x` is not null, `y` otherwise.
27. How to create empty constructor for data class in Kotlin?
If you give default values to all the fields - empty constructor is generated automatically by Kotlin.
data class User(var id: Long = -1,
var uniqueIdentifier: String? = null)
and you can simply call:
val user = User()
Another option is to declare a secondary constructor that has no parameters:
data class User(var id: Long,
var uniqueIdentifier: String?){
constructor() : this(-1, null)
}
28. How would you override default getter for Kotlin data class?
Problem
Given the following Kotlin class:data class Test(val value: Int)
How would I override the Int getter so that it returns 0 if the value negative?
Don't use a data class. Use a regular class.
Answer
Have your business logic that creates the data class alter the value to be 0 or greater before calling the constructor with the bad value. This is probably the best approach for most cases.Don't use a data class. Use a regular class.
data class Test(val value: Int) {
val safeValue: Int
get() = if (value < 0) 0 else value
}
Create an additional safe property on the object that does what you want instead of having a private value that's effectively overriden.
data class Test(val value: Int) {
val safeValue: Int
get() = if (value < 0) 0 else value
}
29. What are Object expressions in Kotlin and when to use them?
Sometimes we need to create an object of some class with slight modification, without explicitly declaring a new subclass for it. Java handles this case with anonymous inner classes. Kotlin uses object expression to achieve the same functionality. We can even create an object expression for an interface or abstract class by just implementing their abstract methods.
It is often used as a substitution to a Java anonymous class:
It is often used as a substitution to a Java anonymous class:
window.addMouseListener(object : MouseAdapter() {
override fun mouseClicked(e: MouseEvent) {
// ...
}
override fun mouseEntered(e: MouseEvent) {
// ...
}
})
30. What is Coroutine Scope and how is that different from Coroutine Context?
Coroutines always execute in some context represented by a value of the CoroutineContext type, defined in the Kotlin standard library. The coroutine context is a set of various elements. The main elements are the Job of the coroutine.
CoroutineScope has no data on its own, it just holds a CoroutineContext. Its key role is as the implicit receiver of the block you pass to launch, async etc.
CoroutineScope has no data on its own, it just holds a CoroutineContext. Its key role is as the implicit receiver of the block you pass to launch, async etc.
runBlocking {
val scope0 = this
// scope0 is the top-level coroutine scope.
scope0.launch {
val scope1 = this
// scope1 inherits its context from scope0. It replaces the Job field
// with its own job, which is a child of the job in scope0.
// It retains the Dispatcher field so the launched coroutine uses
// the dispatcher created by runBlocking.
scope1.launch {
val scope2 = this
// scope2 inherits from scope1
}
}
}
You might say that CoroutineScope formalizes the way the CoroutineContext is inherited. You can see how the CoroutineScope mediates the inheritance of coroutine contexts. If you cancel the job in scope1, this will propagate to scope2 and will cancel the launched job as well.
31. What is inline class in Kotlin and when do we need one? Provide an example.
Sometimes it is necessary for business logic to create a wrapper around some type. However, it introduces runtime overhead due to additional heap allocations. Moreover, if the wrapped type is primitive, the performance hit is terrible, because primitive types are usually heavily optimized by the runtime.
Inline classes provide us with a way to wrap a type, thus adding functionality and creating a new type by itself. As opposed to regular (non-inlined) wrappers, they will benefit from improved performance. This happens because the data is inlined into its usages, and object instantiation is skipped in the resulting compiled code.
Inline classes provide us with a way to wrap a type, thus adding functionality and creating a new type by itself. As opposed to regular (non-inlined) wrappers, they will benefit from improved performance. This happens because the data is inlined into its usages, and object instantiation is skipped in the resulting compiled code.
inline class Name(val s: String) {
val length: Int
get() = s.length
fun greet() {
println("Hello, $s")
}
}
fun main() {
val name = Name("Kotlin")
name.greet() // method `greet` is called as a static method
println(name.length) // property getter is called as a static method
}
Some notes about inline classes:
A single property initialized in the primary constructor is the basic requirement of an inline class
Inline classes allow us to define properties and functions just like regular classes
Init blocks, inner classes, and backing fields are not allowed
Inline classes can inherit only from interfaces
Inline classes are also effectively final
A single property initialized in the primary constructor is the basic requirement of an inline class
Inline classes allow us to define properties and functions just like regular classes
Init blocks, inner classes, and backing fields are not allowed
Inline classes can inherit only from interfaces
Inline classes are also effectively final
32. How Kotlin coroutines are better than RxKotlin/RxJava?
RxJava and its extension, RxKotlin, are an implementation of the Reactive Manifesto, which mandates the applications to be responsive, resilient, elastic, and message-driven. The software that implements these principles uses asynchronous communication with external data sources, applies back-pressure on request producers, and degrades gracefully in case of system failures.
he Kotlin Coroutines library is not a RxKotlin competitor. Its scope is much broader. The Kotlin Coroutines approach consists of two very distinct concepts: the suspend word, which is just a Kotlin language keyword, and the default implementation of that keyword provided by the kotlinx.coroutines library. The suspend keyword guarantees collaborative concurrency: on entering any suspend function, the control is offered to other coroutines that might need it for their continuation.
he Kotlin Coroutines library is not a RxKotlin competitor. Its scope is much broader. The Kotlin Coroutines approach consists of two very distinct concepts: the suspend word, which is just a Kotlin language keyword, and the default implementation of that keyword provided by the kotlinx.coroutines library. The suspend keyword guarantees collaborative concurrency: on entering any suspend function, the control is offered to other coroutines that might need it for their continuation.
33. What is the difference between * and Any in Kotlin generics?
List can contain objects of any type, but only that type, so it can contain Strings (but only Strings)
while List can contain Strings and Integers and whatnot, all in the same list
34. What is the difference between launch/join and async/await in Kotlin coroutines?
launch is used to fire and forget coroutine. It is like starting a new thread. If the code inside the launch terminates with exception, then it is treated like uncaught exception in a thread -- usually printed to stderr in backend JVM applications and crashes Android applications. join is used to wait for completion of the launched coroutine and it does not propagate its exception. However, a crashed child coroutine cancels its parent with the corresponding exception, too.
async is used to start a coroutine that computes some result. The result is represented by an instance of Deferred and you must use await on it. An uncaught exception inside the async code is stored inside the resulting Deferred and is not delivered anywhere else, it will get silently dropped unless processed. You MUST NOT forget about the coroutine you’ve started with async.
async is used to start a coroutine that computes some result. The result is represented by an instance of Deferred and you must use await on it. An uncaught exception inside the async code is stored inside the resulting Deferred and is not delivered anywhere else, it will get silently dropped unless processed. You MUST NOT forget about the coroutine you’ve started with async.
35. What's wrong with that code?
Answer
The problem with this approach is that data classes aren't really meant for altering data like this. They are really just for holding data. Overriding the getter for a data class like this would mean that Test(0) and Test(-1) wouldn't equal one another and would have different hashCodes, but when you called .value, they would have the same result. This is inconsistent, and while it may work for you, other people on your team who see this is a data class, may accidentally misuse it. Let's say I want to override the Int getter so that it returns 0 if the value negative for the data class. What's bad with that approach?data class Test(private val _value: Int) {
val value: Int
get() = if (_value < 0) 0 else _value
}
36. Imagine you moving you
Problem
public class Foo {
private static final Logger LOG = LoggerFactory.getLogger(Foo.class);
}
Answer
Use Static-like approach:class MyClass {
companion object {
val LOG = Logger.getLogger(MyClass::class.java.name)
}
fun foo() {
LOG.warning("Hello from MyClass")
}
}
37. How to implement Builder pattern in Kotlin?
First and foremost, in most cases you don't need to use builders in Kotlin because we have default and named arguments but if you need one use:
class Car( //add private constructor if necessary
val model: String?,
val year: Int
) {
private constructor(builder: Builder) : this(builder.model, builder.year)
class Builder {
var model: String? = null
private set
var year: Int = 0
private set
fun model(model: String) = apply { this.model = model }
fun year(year: Int) = apply { this.year = year }
fun build() = Car(this)
}
}
38. How Kotlin coroutines are better than RxKotlin/RxJava?
Kotlin coroutines are different from Rx. Both are designed to address a problem of asynchronous programming, however their approach to solution is very different:
Rx comes with a particular functional style of programming that can be implemented in virtually any programming language without support from the language itself. It works well when the problem at hand easily decomposes into a sequence of standard operators and not so well otherwise.
Kotlin coroutines provide a language feature that let library writers implement various asynchronous programming styles, including, but not limited to functional reactive style (Rx). With Kotlin coroutines you can also write your asynchronous code in imperative style, in promise/futures-based style, in actor-style, etc.
How Kotlin coroutines are better than RxKotlin? You just write sequential code, everything is as easy as writing synchronous code except it execute asynchronously. It's easier to grasp.
In RxJava you can assign computations to schedulers but subscribeOn() and ObserveOn()are confusing. Every coroutine is given a thread context and return to parent context. For a channel, both side (producer, consumer) execute on his own context. Coroutines are more intuitive on thread or thread pool affectation.
Coroutines give more control on when those computation occur. You can for example pass hand (yield), prioritize (select), parallelize (multiple producer/actor on channel) or lock resource (Mutex) for a given computation. It may not matter on server (where RxJava came first) but on resources limited environment this level of control may be required.
Due to it's reactive nature, backpressure doesn't fit well in RxJava. In the other end send() to channel is a suspensive function that suspend when channel capacity is reached. It's out-of-the-box backpressure given by nature. You could also offer() to channel, in which case the call never suspend but return false in case the channel is full, effectively reproducing onBackpressureDrop() from RxJava. Or you could just write your own custom backpressure logic, which won't be difficult with coroutines, especially compared to do the same with RxJava.
Rx comes with a particular functional style of programming that can be implemented in virtually any programming language without support from the language itself. It works well when the problem at hand easily decomposes into a sequence of standard operators and not so well otherwise.
Kotlin coroutines provide a language feature that let library writers implement various asynchronous programming styles, including, but not limited to functional reactive style (Rx). With Kotlin coroutines you can also write your asynchronous code in imperative style, in promise/futures-based style, in actor-style, etc.
How Kotlin coroutines are better than RxKotlin? You just write sequential code, everything is as easy as writing synchronous code except it execute asynchronously. It's easier to grasp.
In RxJava you can assign computations to schedulers but subscribeOn() and ObserveOn()are confusing. Every coroutine is given a thread context and return to parent context. For a channel, both side (producer, consumer) execute on his own context. Coroutines are more intuitive on thread or thread pool affectation.
Coroutines give more control on when those computation occur. You can for example pass hand (yield), prioritize (select), parallelize (multiple producer/actor on channel) or lock resource (Mutex) for a given computation. It may not matter on server (where RxJava came first) but on resources limited environment this level of control may be required.
Due to it's reactive nature, backpressure doesn't fit well in RxJava. In the other end send() to channel is a suspensive function that suspend when channel capacity is reached. It's out-of-the-box backpressure given by nature. You could also offer() to channel, in which case the call never suspend but return false in case the channel is full, effectively reproducing onBackpressureDrop() from RxJava. Or you could just write your own custom backpressure logic, which won't be difficult with coroutines, especially compared to do the same with RxJava.
39. What are the advantages of Kotlin over Java?
Basically for me less thinking required to write kotlin equivalent to most java code:
java: rewrite the getter and setter for each variable you use it for
kotlin: don't have to write getter and setter, and custom getter and setter take a lot less typing in kotlin if you do want to. also delegates exist for identical getters\setters
kotlin: does exist, makes functions more clear in usage and feels more natural.
kotlin: you get to decide what can and cant be null. allows for nice things like inline class
kotlin: object instead of class
kotlin: Reified generics (you can access the actual type), in and out for covariance
kotlin: does exist, easy to preserve api back-compatiblity.
kotlin: very nice to be able to quickly write a constructor without any constructor function or extra needless declarations
data class
java: you have to write getters and setters for each thing, you have to write hashCode properly (or let IDE auto generate, which you have to do again every time you change the class), toString (same problem as hashcode) and equals (same problem as hashCode). or you could use lombok, but that comes with some quirky problems of its own. record types are hopefully on the way. *kotlin: data class does it all for you.java: rewrite the getter and setter for each variable you use it for
kotlin: don't have to write getter and setter, and custom getter and setter take a lot less typing in kotlin if you do want to. also delegates exist for identical getters\setters
abstract vs open classes
java: you have to make an abstract class implementation kotlin: open class lets you make an inheritable class while also being usable itself. nice mix of interface and regular class imoextension functions
java: doesnt existkotlin: does exist, makes functions more clear in usage and feels more natural.
null
java: Anything but primitives can be null at any time.kotlin: you get to decide what can and cant be null. allows for nice things like inline class
singleton
java: Memorize singleton patternkotlin: object instead of class
generics
java: Theyre alright, nothing fancykotlin: Reified generics (you can access the actual type), in and out for covariance
named parameters
java: does not exist, easy to break api back-compatibility if you arent careful.kotlin: does exist, easy to preserve api back-compatiblity.
primary constructor
java: does not have per-se, you still have to define everything inside the classkotlin: very nice to be able to quickly write a constructor without any constructor function or extra needless declarations
40. When to use lateinit over lazy initialization in Kotlin?
There are some simple rules to determined if you should use one or the other for properties initialisation:
If properties are mutable (i.e. might change at a later stage) use lateInit
If properties are set externally (e.g. need to pass in some external variable to set it), use lateinit. There’s still workaround to use lazy but not as direct.
If they are only meant to initialized once and shared by all, and it’s more internally set (dependent on variable internal to the class), then use lazy. Tactically, you could still use lateinit, but using lazy would better encapsulate your initialization code.
If properties are mutable (i.e. might change at a later stage) use lateInit
If properties are set externally (e.g. need to pass in some external variable to set it), use lateinit. There’s still workaround to use lazy but not as direct.
If they are only meant to initialized once and shared by all, and it’s more internally set (dependent on variable internal to the class), then use lazy. Tactically, you could still use lateinit, but using lazy would better encapsulate your initialization code.