Kotlin Coroutines and Why They Are Necessary

Kotlin coroutines are very useful. In this article we will go through basic examples of using coroutines, and we will also examine what happens under the hood. Why are solutions like Coroutines necessary? Handling execution on multiple threads is unavoidable in modern application development. Drawing fancy loaders on the UI while waiting for a network […]

Read More

Things to Know about Kotlin Extension Functions

How many times have you wished there was a simpler way to add just a small piece of functionality to a class in Java? Whenever that happened, you might have ended up either extending the class or utility static method. Both works fine, but it feels like there should be something more convenient, right? If […]

Read More

What is the best way to do a reverse lookup on an enum in Kotlin?

I’m trying to find the best way to do a ‘reverse lookup’ on an enum in Kotlin. One of my takeaways from Effective Java was that you introduce a static map inside the enum to handle the reverse lookup. Porting this over to Kotlin with a simple enum leads me to code that looks like […]

Read More

What is the difference between List and Array types in Kotlin?

What is the difference between List and Array types? It seems like we can perform the same operations with them (loops, filter expression, etc..), is there any difference in behavior or usage? val names1 = listOf(“Joe”,”Ben”,”Thomas”) val names2 = arrayOf(“Joe”,”Ben”,”Thomas”) for (name in names1) println(name) for (name in names2) println(name) Solution Arrays and lists (represented by List<T> and its subtype MutableList<T>) have many differences, […]

Read More

How to sort based on/compare multiple values in Kotlin?

Say I have a class Foo(val a: String, val b: Int, val c: Date) and I want to sort a list of Foos based on all three properties. How would I go about this? Solution Kotlin’s stdlib offers a number of useful helper methods for this. First, you can define a comparator using the compareBy() method and pass it to […]

Read More

How to use Data Binding and Kotlin in Android Studio 3.0.0

I just started to use Android Studio 3.0.0, but every time I try to build my project I get this error: Error:Circular dependency between the following tasks: :app:compileDebugKotlin +— :app:dataBindingExportBuildInfoDebug | — :app:compileDebugKotlin (*) — :app:kaptDebugKotlin — :app:dataBindingExportBuildInfoDebug (*) (*) – details omitted (listed previously) I am using kapt “com.android.databinding:compiler:2.2.0” Before I was using androidProcessor […]

Read More

What is the equivalent of Java static methods in Kotlin?

There is no static keyword in Kotlin. What is the best way to represent a static Java method in Kotlin? Solution You place the function in the “companion object”. So the java code like this: class Foo { public static int a() { return 1; } } will become class Foo { companion object { […]

Read More

Why do we use “companion object” as a kind of replacement for Java static fields in Kotlin?

What is the intended meaning of “companion object”? So far I have been using it just to replace Java’s static when I need it. I am confused with: Why is it called “companion”? Does it mean that to create multiple static properties, I have to group it together inside companion object block? To instantly create […]

Read More

Kotlin – Property initialization using “by lazy” vs. “lateinit”

In Kotlin if you don’t want to initiate a class property inside the constructor or in the top of the class body, you have basically these two options ( from the language reference): Lazy Initialization lazy() is a function that takes a lambda and returns an instance of Lazy which can serve as a delegate […]

Read More

How to convert a kotlin source file to a java source file

I have a Kotlin source file, but I want to translate it to Java. How can I convert Kotlin to Java source? Solution In IntelliJ or Android Studio, you just have to do the following to get java code from kotlin: Menu>Tools>Kotlin>Show Kotlin Bytecode Click on the Decompile button Copy the java code This question was reposted […]

Read More