1. Overview

Kotlin is built to be interoperable with Java. This means that Kotlin applications can have both Kotlin and Java source files.

Sometimes we might want to convert Java code to Kotlin. In this tutorial, we’ll see various ways to do that.

2. Advantages of Kotlin Over Java

Let’s quickly understand a few benefits of Kotlin over Java, as that might also guide our decision on whether we want to convert Java code to Kotlin:

3. Different Ways to Convert Java Code to Kotlin

We can convert our Java code to Kotlin without explicitly rewriting the Kotlin code equivalent.

Now, let’s see various approaches.

3.1. Using IntelliJ IDE Software or Android Studio Copy/Paste Method

We can use both Android Studio and IntelliJ IDE to convert our Java code to Kotlin code.

First, we must create or have an existing Kotlin project. We open this project on either IDE and within a .kt file, we paste some Java code. Then, the IDE prompts us with a dialog asking if we wish to convert the Java code to Kotlin.

Suppose, we wish to paste this piece of Java code. A simple function to add two numbers and return their sum:

public class TestClass {
    public static void main(String[] args) {
        System.out.println("Sum demo " + sum(2, 4));
        System.out.println("Product demo " + product(2, 4));
    public static int sum(int x, int y) {
        int a = x;
	int b = y;
	return a + b;
    public static int product(int x, int y) {
	int a = x;
	int b = y;
	return a * b;

Now, pasting this code into our .kt file on IntelliJ displays a prompt:

IntelliJ Dialog when copying Java code into a Kotlin project

Now, we click on Yes to allow IntelliJ to perform the code conversion.

By the end of the conversion, the Java code will be replaced by the Kotlin code in the same file as seen in the image below:

Kotlin classes derived from Java class converted on IntelliJ IDE

It’s important to note that this method is very much convenient for converting snippets of Java code at a time.

However, having to paste Java code into the IDE for automatic conversion to Kotlin means a slower process when having to convert a large amount of Java code. Also, this method may not work well for Kotlin projects with existing Java source code files.

3.2. Using IntelliJ IDE Software or Android Studio Menu Option Method

Another way to perform the conversion using IntelliJ or Android Studio IDE software is to create a .java file in our Kotlin project.

Let’s create two Java classes, Summation, and Product,  in our project:

Java classes to be converted to Kotlin Via Menu option

To convert these Java classes to Kotlin, we can navigate to the Code -> Convert Java File to Kotlin File menu entry:

Options menu UI on IntelliJ or Android Studio IDE

Once the conversion process finishes, we get a .kt file with the same name as the Java class. The .java files will then be removed from the project folder:

Kotlin Classes derived from Java classes via Menu option

Besides using the navigation menu, we also have keyboard shortcuts for Mac and Windows to achieve the same result: Shift + Alt + Cmd + k (in Mac) and Shift + Alt + Ctrl + k (in Windows).

This method is very convenient for converting all Java source files to Kotlin in a mixed Java/Kotlin project. It performs the conversion quickly and project-wide.

3.3. Using JavaInUse Online

JavaInUse is a great online tool we can also use to convert Java code to Kotlin. All we need to do is write or paste the Java code we intend to convert into the left (editable) text box, and then click the Submit button to perform the conversion:

Use of JavaInUse tool to convert Java to Kotlin

Using this online tool, however, has some downsides. Firstly, we can only convert a chunk of Java code at a time. This means that we can’t convert all Java code to Kotlin by uploading the project folder. We must perform the conversion in bits or per Java class.

Secondly, there are security risks in uploading our Java code since this is a third-party site.

Lastly, we recommend this tool for simple Java code conversions. It may not work if we try to convert Java code with many methods, method calls, and complex constructs.

4. Conclusion

This article shows two major ways to convert Java code to Kotlin. Firstly, we can use Android Studio or IntelliJ IDE software, and secondly via an online tool.

We also discussed the limitations of both approaches. The first methods work only with IntelliJ and Android Studio IDE software, while the online tool can be used with any editor.

Comments are closed on this article!