Using the Kotlin stdlib in Java code

Victoria Gonda
victoriagonda
Published in
3 min readDec 3, 2018

--

Kotlin introduces a lot of great helper methods for us. Some of these include String helpers such as this one to capitalize the first character of a String.

"some string".capitalize()

Many of us have created something such as a StringUtils class in Java to build our own of these methods before we were using Kotlin. One of the great things about Kotlin is that it eliminates the need for many of these util classes! What’s more, is that Kotlin has extension functions so it gives the illusion that any of these helper methods we do make we can call on an instance of an object.

This is awesome. However, many of us are working in mixed projects, using both Kotlin and Java. Because of that, we might be tempted to keep around our Util classes for use in the Java parts of our code. This seems unfair, but what else are we expected to do? This bit of code duplication seems necessary if our project isn’t fully in Kotlin.

public static String capitalize(String original) {
if (TextUtils.isEmpty(original)) {
return original;
}

return original.substring(0, 1).toUpperCase() +
original.substring(1);
}

I don’t like this duplication. Kotlin already provides this method, and we already have Kotlin in our project. There must be a way reuse this, right? The interop is so great otherwise.

Kotlin uses Java String and is able to provide these additional functions through the use of extension functions. If you decompile the code, extension functions are little more than the static utility methods you make in Java. Another thing we can learn is that the class name is the same as the file name.

From this information, we should be able to use these static functions in our Java as well! To do this, we have to know the name. For these String extensions, they are found in StringsKt. Import that, and you can use them all!

import kotlin.text.StringsKt;

In Java you can’t use the same syntax to call the extension function right on the object. It should be used just like a static method here, considering that’s exactly what it is.

StringsKt.capitalize(day)

Yay! You can now eliminate some of those util methods and use Kotlin extensions in those places.

Where are these functions?

So that’s where the String extensions are. How did I find them, and how can you find the extensions for other classes? Answer: let the editor help you!

There are probably a number of ways you can locate these, but I let Android Studio help me. This will work in IntelliJ too.

To start with, I looked at the extension function I was looking for with the autocomplete in a Kotlin file. In the dropdown by the capitalize function there’s some addition data “for String in kotlin.text.” Especially that “kotlin.text” should look familiar from the import.

Kotlin autocomplete for String.capitalize()

Then I moved to my Java file. I know that unless there was an annotation placed otherwise the class is named after the filename. From the information above “StringKt” is a good guess. The autocomplete has “StringsKt” as an option, so that’s a good bet.

Java autocomplete for StringKt

This turns out to be the exact class I needed to call that extension function! I hope you found this helpful. Do you have any other tricks you like using?

Header photo by Gary Chan on Unsplash

--

--