Convert Java Code to Kotlin Code

Many projects and applications are coded using Java programming language. In a Kotlin application, you can have Java files along with Kotlin files. But, if you like to convert your Java code to Kotlin code, you may do so.

To migrate from Java to Kotlin, Kotlin Official is providing tools via online, which is using official Kotlin site, or offline, which is using IntelliJ IDEA integrated development environment.

A quick route for the above said methods is given below.

  1. Use IntelliJ IDEA
    Main menu -> Code -> Convert Java File to Kotlin File
  2. You can use the online converter from Kotlin Official Website.
    https://try.kotlinlang.org/ -> Convert from Java

Of course, we shall go in detail using a step by step process and screenshots to better understand the process to convert Java code to Kotlin code.

Note: If you are using IntelliJ IDEA or any other IDE with Kotlin Plugin, you can write Kotlin code along with Java code in the same .kt file.

Use IntelliJ IDEA – Convert Java File to Kotlin File

IntelliJ IDEA is an Integrated Development Environment by JetBrains.

Following are the steps to convert your Java File to Kotlin File using IntelliJ IDEA,

Step 1: Open your Project in IntelliJ IDEA, and open the Java file.

Open Java File in IntelliJ IDEA - Kotlin Tutorial - www.tutorialkart.com
Open Java File in IntelliJ IDEA

Step 2: In the main menu bar, click on “Code”. Click on the item “Convert Java File to Kotlin File”

Main Menu -> Code -> Convert Java File to Kotlin File - Kotlin Tutorial - www.tutorialkart.com
Main Menu -> Code -> Convert Java File to Kotlin File

Step 3: If you get a message “Some code in the rest of your project may require corrections after performing this conversion. Do you still want to find such code and correct it too?”, you may click OK.

Some code in the rest of your project may require corrections after performing this conversion. Do you still want to find such code and correct it too - Click OK - Kotlin Tutorial - www.tutorialkart.com
Information Message

Step 4: Finally, your Java file is converted to a Kotlin file. Try running it.

Java File is converted to Kotlin File - Kotlin Tutorial - www.tutorialkart.com
Java File is converted to Kotlin File

Try Online “Convert from Java”

Step 1: Hit the URL “https://try.kotlinlang.org/” in your favorite browser and click on “Convert from Java” button, highlighted in the below picture.

hit the url https://try.kotlinlang.org/ - Kotlin Tutorial
Hit the url

Step 2: Paste you Java Code in the left editor box and click on “Convert to Kotlin” button present in the bottom right corner of the window.

Convert to Kotlin Window - Kotlin Tutorial - www.tutorialkart.com
Convert to Kotlin Window

Conclusion

In this Kotlin Tutorial, we learned how to convert Java File to Kotlin File.

If you are doing Android application development using Kotlin, you may refer how to convert Java File in Android application to Kotlin File.