Kotlin classes in Java world

Peng Jiang
ASOS Tech Blog
Published in
5 min readFeb 1, 2019
Photo by slon_dot_pics from Pexels

Kotlin 1.3 was released recently with a ton of new features and functionalities. One of the new features introduced is the inline class declaration (still in experimental). As more data types are introduced in Kotlin, this intrigued me enough to check how they look in Java world. In this post, I would like to go through the data types in Kotlin and show you the corresponding Java implementations.

The following Java classes are converted by IntelliJ Kotlin ByteCode decompiler, with inline/optimization and JVM 8 target option selected. To make it easier to read, the annotations for the Java classes are also removed.

Classes

Classes in Kotlin are very similar to the class in Java. Taking this User class as an example, the first difference you will notice is the final will be the default access modifier for the Kotlin class. Kotlin classes also automatically generate some boilerplate code, such as getter methods and a default constructor.

Class in Kotlin and Java

Data class

As its name suggests, the primary purpose of data class is to hold data. It can’t be abstract or open. Data class removes lots of boilerplate code and gives a lot of benefits. The data class in Java code, still extends from the basic class and contains all automatically generated hash code, equal, getter, setter, etc. You can see how much boilerplate we can get rid of by moving to Kotlin data classes.

Data class in Kotlin And Java

Except for the methods I just mentioned, the component and copy methods are also created. The copy method will be really useful when you want to reuse values from an existing instance. It lets you create new instances of a data class, overriding the parameters with what you want.

The component methods are used in a destructuring declaration. Sometimes it’s convenient to destructure an object into a number of variables, for example, if you want to get thefirstName and lastNamefrom a UserInfo instance, you can do:

val customerInfo = CustomerInfo("Kotlin", "Java")
(firstName, lastName) = customerInfo

A destructuring declaration creates these variables all at once. We have declared two new variables here: firstName and lastName, and can use them independently. By convention, any instance of a class that has a series of methods named component1(), component2() and so on can be used in a destructuring declaration.

Enum class

Enum class in Kotlin is also very similar to the Java Enum type, which enables a variable to be a set of predefined constants. For a simple enum class, it looks exactly the same, but when you want to put a value in the class, Kotlin Enum class is simpler, which is similar to the data class.

Enum class in Kotlin and Java

Sealed class

A sealed class in Kotlin is used to represent the restricted class hierarchies. A sealed class is abstract by itself, it cannot be instantiated directly. The top-level class is an abstract class, which has a private constructor. The subclass will use the same approach as I mentioned above to extend the sealed class. To make it part of the sealed class, the subclass will be implemented with a static nest class approach.

Seal class in Kotlin and Java

Inline class

Inline class is introduced in Kotlin 1.3 as an experimental feature, which removes additional heap allocations for the run-time overhead. If you want to try it in your project, you need to set the compiler argument in the gradle build setting:

compileKotlin {
kotlinOptions.freeCompilerArgs += ["-XXLanguage:+InlineClasses"]
}

At run-time, instances of the inline class will be represented using this single property. It’s very interesting to see the implementation in Java. Taking this simple Password class as an example, it has a private constructor but with a static constructor method to create an instance (boxing) and a static method to get the value. There are two implements for the hashCode, toString, equals. The normal methods are for the inline class and the static methods are used for the value property.

Inline class in Kotlin and Java

Object

Object has two meanings in Kotlin. First, the object expression, which creates a slight modification object of some class or implements an interface, without explicitly declaring a new subclass/class for it. Very similar to the anonymous inner classes in Java. The other is object declaration, which makes it easy to declare singletons in Kotlin. Here, we focus on the object declaration, where you may have interoperation between Java and Kotlin. The object declaration is initialised lazily, when accessed for the first time. It also handles the thread issue when creating the object. If you use JvmStatic annotation for a method or a variable, the compiler will generate both a static method in the enclosing class of the object and an instance method in the object itself.

Object declaration in Kotlin and Java

In this post, we explored the Kotlin type system and how it helps us to write better and safer code. For the Android team at ASOS, Kotlin is always preferred when implementing a new feature — we have more than 40% of the code in Kotlin on our Android app now. Although Kotlin is becoming more and more popular, we’re still in the JVM ecosystem and need to handle legacy code or call libraries implemented in Java. The Java implementations give you an idea when you want to call Kotlin code from Java, especially when Java and Kotlin are mixed in the same code base.

My name is Peng Jiang. I’m always passionate about simple and well-crafted code. I currently work as a Lead Android Engineer at ASOS. In my spare time, I also play with Swift and Flutter. At ASOS, we’re always looking for strong, friendly and talented developers. You’ll only need to write Kotlin here. If that sounds interesting to you, do get in touch!

--

--