How to Convert a Java File Code to Kotlin

Kailash Vaviya Feb 02, 2024
  1. Why Convert Java Source File to Kotlin
  2. Convert Entire Java File to Kotlin
  3. Convert a Few Java Code Lines to Kotlin
How to Convert a Java File Code to Kotlin

Kotlin is now the official Android development language, as declared by Google in 2017. Hence, it is evident that developers might wish to convert their Java code to Kotlin.

If you wonder how to do that, this article will introduce some basic steps to convert your Java file to Kotlin.

There are two scenarios where you might look out to convert Java to Kotlin: converting the entire file or converting a piece of code.

But before getting into converting Java source files to Kotlin, let’s understand why someone would want to do so.

Why Convert Java Source File to Kotlin

There are many reasons a developer would like to convert Java to Kotlin. The primary reason is that Kotlin is now the official Android development language.

Besides that, Kotlin offers various benefits over Java, such as:

  • Kotlin is easier to read and understand than Java.
  • Both use the same JVM compiler.
  • Kotlin can handle significant exceptions like the null pointer exception.
  • Kotlin code is cleaner.

These reasons are good enough for a developer to choose Kotlin over Java and convert the source code.

Convert Entire Java File to Kotlin

Since Kotlin is now the official Android development language, converting Java files to Kotlin has become easy.

Now, there are again two scenarios here. You will either be using Android Studio 3.0 or the earlier versions.

We will go through both scenarios in this article.

Convert Java to Kotlin Using Android Studio 3.0

When using Android Studio 3.0, you don’t have to install additional plugins. Here’s the step-by-step guide to converting the entire Java file to Kotlin.

  1. You first need to open the Java file you want to change in the Android Studio.

  2. Once the file is open, navigate to Android Studio Menu -> Code -> Convert Java File to Kotlin File. Alternatively, you can also use the shortcut keys Ctrl+Alt+Shift+K.

    Navigation to Convert Java File to Kotlin

  3. A dialogue box will open to ask permission for configuring Kotlin in the project and convert the code.

    Kotlin Configuration Permission

  1. Click on OK to give permission and let Android Studio change the build.gradle file and convert the Java file to Kotlin.

    Configuring Kotlin in Android Studio

Convert Java to Kotlin in Earlier Android Studio Versions Before 3.0

For the versions before Android Studio 3.0, we need to install the Kotlin plugin to complete the conversion.

To install the plugin, navigate to Android Studio Menu -> File -> Settings -> Plugins -> Browse Repositories -> Kotlin.

Once the plugin is installed, open the Java file you wish to convert and follow the four steps mentioned above to convert Java to Kotlin in Android Studio 3.0.

Convert a Few Java Code Lines to Kotlin

There might be situations where you might desire to convert only a piece of code and not the entire file. In that case, the best way to convert is:

  1. Open the Kotlin file with the .kt extension where you wish to convert the code.
  2. Open the Java file and copy the code you desire to convert to Kotlin.
  3. Paste the Java code in the Kotlin file.
  4. Android Studio will recognize the Java code and ask whether you wish to convert it to Kotlin.
  5. Allow the Android Studio to make the conversion, and it will convert your Java code to Kotlin.
Kailash Vaviya avatar Kailash Vaviya avatar

Kailash Vaviya is a freelance writer who started writing in 2019 and has never stopped since then as he fell in love with it. He has a soft corner for technology and likes to read, learn, and write about it. His content is focused on providing information to help build a brand presence and gain engagement.

LinkedIn

Related Article - Kotlin Java