How do I check if two Strings are equal valued in Kotlin?

How do I check if two Strings are equal valued in Kotlin? Solution Using == (double equal to) operator. fun main(args: Array<String>) { val a: String = “kotlin is easy” val b: String = “kotlin is” + ” easy” if (a == b) { println(” a and b are equal.”) } else { println(” a […]

Read More

Kotlin – Throw Custom Exception

How can I throw a custom exception in Kotlin? Solution If you want to create a custom Exception, just extend Exception class like: class CustomException(message: String) : Exception(message) fun main(args: Array<String>) { throw CustomException(“Error!”) // Throw Exception in thread “main” }

Read More

Format in Kotlin string templates

Kotlin has an excellent feature called string templates. val i = 10 val s = “i = $i” // evaluates to “i = 10” But is it possible to have any formatting in the templates? For example I would like to format Double in string templates in kotlin, at least to set a number of […]

Read More

How do I run a Kotlin class on command line?

I can’t run a compiled Kotlin class. When I try to run it like I would a normal Java class I get the following: C:\Users\User\Desktop>java _DefaultPackage Exception in thread “main” java.lang.NoClassDefFoundError: jet/runtime/Intrinsics at _DefaultPackage.main(Finder.kt) Caused by: java.lang.ClassNotFoundException: jet.runtime.Intrinsics at java.net.URLClassLoader$1.run(URLClassLoader.java:366) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:423) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:356) … 1 […]

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 are some pros and cons of using Kotlin?

Kotlin is recommended in all circumstances where you would use Java. It largely uses the same concepts and libraries, however, it offers many new syntactic constructs which streamline common boilerplate patterns a lot. This often results in up to 25–35% less source code, but the generated bytecode remains about the same size and efficiency, and there […]

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