Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
How to Convert Kotlin Code to Java Code in Android Studio?
Next article icon

How to Convert Kotlin Code to Java Code in Android Studio?

Last Updated : 06 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Java programming language is the oldest and most preferred language for Android app development. However, during Google I/O 2017, Kotlin has been declared as an official language for Android development by the Google Android Team. Kotlin has gained popularity among developers very quickly because of its similarities as well as interoperable with the Java language. One can mix code of Java and Kotlin while designing an Android project. The syntax of Java and Kotlin differs in many aspects but their compilation process is almost the same. Code of both the languages gets compiled into bytecode that is executable on Java Virtual Machine(JVM). Thus if one can derive the bytecode of compiled Kotlin file, it can be decompiled in order to produce the equivalent Java code. Android Studio does exactly the same to carry out the code conversion from Kotlin to Java. Developers may have many reasons to convert the Kotlin code into Java such as:

  • To integrate features that are easy to implement in Java language.
  • To resolve some performance issue that is difficult to locate in Kotlin.
  • To remove the Kotlin code from the project files.
Convert Kotlin Code to Java Code in Android Studio

Code Conversion

Step 1: Open Kotlin Class/File

Open the Kotlin Class/File which is to be converted into Java. Consider the code of the MainActivity file mentioned below for the conversion.

Kotlin
import android.os.Bundle import android.view.View import android.widget.TextView import androidx.appcompat.app.AppCompatActivity  class MainActivity : AppCompatActivity() {     // declaring variable for TextView component     private var textView: TextView? = null      // declaring variable to store     // the number of button click     private var count = 0     override fun onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)         setContentView(R.layout.activity_main)          // assigning ID of textView2 to the variable         textView = findViewById(R.id.textView2)          // initializing the value of count with 0         count = 0     }      // function to perform operations     // when button is clicked     fun buttonOnClick(view: View?) {         // increasing count by one on         // each tap on the button         count++          // changing the value of the         // textView with the current         // value of count variable         textView!!.text = Integer.toString(count)     } } 

Step 2: Navigate to Tools Menu

From the topmost toolbar of the Android Studio, select Tools and then navigate to Kotlin > Show Kotlin Bytecode. It will open a window at the right-hand side that will contain the line by line bytecode for the Kotlin file. 

Step 3: Decompile bytecode

In the bytecode window, checkbox the option "JVM 8 target" and click on Decompile. The Android Studio will generate the Java equivalent code for the Kotlin file. The produced java code will contain some additional information like metadata. Below is the generated Java code for the above mentioned Kotlin file.

Java
import android.os.Bundle; import android.view.View; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import kotlin.Metadata; import kotlin.jvm.internal.Intrinsics; import org.jetbrains.annotations.Nullable;  @Metadata(    mv = {1, 4, 1},    bv = {1, 0, 3},    k = 1,    d1 = {"\u0000,\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\b\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0000\u0018\u00002\u00020\u0001B\u0005¢\u0006\u0002\u0010\u0002J\u0010\u0010\u0007\u001a\u00020\b2\b\u0010\t\u001a\u0004\u0018\u00010\nJ\u0012\u0010\u000b\u001a\u00020\b2\b\u0010\f\u001a\u0004\u0018\u00010\rH\u0014R\u000e\u0010\u0003\u001a\u00020\u0004X\u0082\u000e¢\u0006\u0002\n\u0000R\u0010\u0010\u0005\u001a\u0004\u0018\u00010\u0006X\u0082\u000e¢\u0006\u0002\n\u0000¨\u0006\u000e"},    d2 = {"Lcom/example/javatokotlin/MainActivity;", "Landroidx/appcompat/app/AppCompatActivity;", "()V", "count", "", "textView", "Landroid/widget/TextView;", "buttonOnClick", "", "view", "Landroid/view/View;", "onCreate", "savedInstanceState", "Landroid/os/Bundle;", "app"} ) public final class MainActivity extends AppCompatActivity {    private TextView textView;    private int count;     protected void onCreate(@Nullable Bundle savedInstanceState) {       super.onCreate(savedInstanceState);       this.setContentView(1300009);       this.textView = (TextView)this.findViewById(1000069);       this.count = 0;    }     public final void buttonOnClick(@Nullable View view) {       int var10001 = this.count++;       TextView var10000 = this.textView;       Intrinsics.checkNotNull(var10000);       var10000.setText((CharSequence)Integer.toString(this.count));    } } 

Note: The Kotlin to Java code conversion will not create a new file in the project directory from where one can access the Java code. Thus to use the Android Studio generated Java code, one needs to copy it from the displayed decompiled java file.

Advantages of Java Over Kotlin

  • Operator overloading is not possible.
  • Classes written in Java are not made final by default.
  • More readable syntax.
  • Use of static methods and variables.

Next Article
How to Convert Kotlin Code to Java Code in Android Studio?

R

RISHU_MISHRA
Improve
Article Tags :
  • Java
  • Technical Scripter
  • TechTips
  • Kotlin
  • Android
  • Technical Scripter 2020
Practice Tags :
  • Java

Similar Reads

    How to Convert Java Code to Kotlin Code in Android Studio?
    In Google I/O 2017, Kotlin has been declared as an official language for Android app development. This language gains popularity among developers very quickly because of its similarities as well as interoperable with Java language. One can mix code of Java and Kotlin while designing an Android proje
    4 min read
    How to Convert a Kotlin Source File to a Java Source File in Android?
    We use Android Studio to translate our Java code into Kotlin while transitioning from Java to Kotlin. But what if we need to convert a Kotlin file to its Java equivalent? We'll examine how to convert a Kotlin source file to a Java source file in this blog. Let's get this party started. Because of it
    2 min read
    How to Build a Roman Numeral Convertor in Android Studio?
    Roman Numeral converter is an app through which we can convert a decimal number to its corresponding roman number or a roman number to its corresponding decimal number in the range of 1 to 3999. The user will enter a decimal number and on clicking the convert to roman numeral button, the entered num
    5 min read
    How to Clone Android Project from GitHub in Android Studio?
    Android Studio is the official IDE for Android app development, based on IntelliJ IDEA. It supports building apps for Android phones, tablets, Wear OS, TV, and Auto using Java or Kotlin for backend and XML for UI design. Git is an open-source version control system that tracks changes in code, makin
    2 min read
    How to Create Classes in Android Studio?
    In Android during the project development most of the time there is a need for classes in the projects. For example, in the case of CRUD operation, we need a model class to insert and retrieve data. Also to hold the info in our custom view we need to create a getter setter class. So basically in and
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences