Start Using Kotlin Today

One of the great wonders of Kotlin is that it’s fully integrated with Java. This means that although all your application code is written Java, you can create a class in Kotlin and use it from Java without any issues.

This potentially gives you two advantages:

  • You can use Kotlin in a Java project: In any project you have already started, you can decide to start writing the new code in Kotlin. You can then call it from Java code.
  • If you have a mental block in Kotlin, you can do that part in Java: many people ask me if there is a case where Kotlin won’t be enough to do something on Android. In theory, everything can be done, but in practice there is no way to know (nobody has done “everything” on Android with Kotlin). The fact is that it doesn’t matter, if you can’t do it in Kotlin, you can implement that part in Java.

Today we’re going to see how this compatibility works, and Kotlin code looks when used from Java.

Package-level functions

In Kotlin, functions don’t need to be inside a class, but this isn’t the case in Java. How can we call a function then? Imagine that we have a file utils.kt that looks like this:

In Java we can access them through a class that will be called UtilsKt, with some static methods:

You’ve already seen from previous articles that I love extension functions. How do they look in Java? Imagine that we have the following:

Note: although they may have appeared at some point, I haven’t explicitly commented on it. The arguments of a function may have default values. This means that if we don’t specify them, they’ll take the value specified in the declaration. This prevents us from using method overloading, as we tend to use in Java.

The function is applied over a ViewGroup. It receives a layout and inflates it using the parent view.

What would we get if we want to use it in Java?

As you can see, the object that applies this function (the receiver) is added as an argument to the function. In addition, the optional argument becomes mandatory, because in Java we can’t use default values.

If you want to generate the corresponding overloads in Java, you can use @JvmOverloads annotation for that function. That way, you wouldn’t need to specify the false in Java:

If you prefer to specify the name of the class when used in Java, you can use an annotation to modify it. In the file utils.kt, add this before the package:

And now the class in Java will be named:

Instance and static fields

In Java we use fields to store the state. They can be instance fields, which means that each object will have its own, or static (all instances of a class will share them).

If we try to find a mapping of this in Kotlin, it’d be the properties and the companion objects. If we have a class like this:

How will this work in Java? You can simply access the companion objects as static fields, and properties using getters and setters:

And you’ll see that the compiler doesn’t complain. Being val, it only generates the getter in Java. If it were var, we’d also have a setter.

Access to instance has worked automatically because it uses the lateinitannotation, which automatically exposes the field that Kotlin uses to store the state. But imagine we create a constant:

You’ll see that you can’t access to it directly. You will have to access through a Companion internal  class:

Which doesn’t look very good. To expose the field on Java the same way a static field would look, you’ll need a new annotation:

And now you can use it from Java code:

If you have functions in a companion object, they’re converted to static methods using the @JvmStatic annotation.

Conclusion

You see that it’s very simple to use the Kotlin code from Java. Here I’ve shown some of the most typical cases, but everything can be achieved in a very similar way.

I hope that if you had any doubts, this convinced you to start using Kotlin in your projects. And if you really want to get serious, keep following here for more. We will start getting our hands dirty by doing a very simple Kotlin App from scratch and cover mosts of Android Development and finally publish our application to the Google Play Store.

Happy Coding!

 

Juma Allan

In this blog, I write tutorials and articles related to Android and Node Js programming. I am an Open Source Enthusiast and have been programming Android for three years now. I love sharing my knowledge with others, and learning new languages everyday. I write on this blog to share what I've gained by learning from the web and friends.