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

Kotlin 1.1.50 is out

We’re happy to announce the release of Kotlin 1.1.50, a new bugfix and tooling update for Kotlin 1.1. This update: Introduces a new versioning scheme: 1.1.5x instead of 1.1.5-x Improves support for JSR-305 annotations (nullability problems can be reported as warnings, checks work when JSR-305 classes are not on the classpath) Improves generated bytecode performance […]

Read More

Organizing Code with Packages in Kotlin

In this tutorial, you’ll learn how to organize your code using packages in Kotlin. Packages In Java we use packages to group related classes; for example, the java.util package has a number of useful utility classes. Packages are declared with the package keyword, and any Kotlin file with a package declaration at the beginning can contain declarations of classes, functions, or […]

Read More

Kotlin Singletons and Object Declarations

A singleton is a class which only allows one instance of itself to be created. This instance needs to also be globally accessible. Singletons are useful when exactly one object is needed to coordinate actions across the system. Object Declarations Kotlin provides an easy way to create singletons using the object declaration feature. For that, object keyword […]

Read More

How to Write Concise Code with Kotlin

One of the major selling points of Kotlin is its concise and expressive syntax. But in how far does Kotlin actually allow you to write more concise code? Let’s look at four ways it accomplishes this. Data Classes In Java, you sometimes create classes which act simply as data containers without much additional functionality. This may […]

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

Data Types in Kotlin

A variable is a location in memory that holds data. This tutorial will help you learn about variables in Kotlin, how they are created, and the basic data types that Kotlin supports for creating these variables. Declaring a variable in Kotlin To declare a variable in Kotlin, either var or val keyword can be used. Here is an example: […]

Read More

Hello World in Kotlin vs Java

The customary “Hello, World!” is a simple program that outputs Hello, World! on the screen. It is a very simple program and is often used to introduce a new programming language. Kotlin “Hello, World!” Program // Outputs Hello World fun main(args : Array<String>) { println(“Hello, World!”) } When you run the program, the output will be: Hello, […]

Read More

KotlinConf – New Workshop and Community Event by JetBrains and Realm

KotlinConf is just around the corner, and if you haven’t checked out the great speaker line-up and sessions, you might want to do that now! We’ve got two days jam-packed with content around Kotlin, whether you’re doing mobile, desktop, server-side or web front-end development, there are lots of talks for you. And we are now […]

Read More