Javatpoint Logo
Javatpoint Logo

Is Java Interpreted or Compiled

The Java programming language was developed in the early 1990s by Sun Microsystem. Java is an object-oriented, simple, efficient, robust, and general-purpose programming language. It is primarily used for web-based enterprise applications. It was initially designed for embedded network applications running on different platforms.

When we start learning Java programming, one question arises: whether Java is interpreted or Compiled, or both. Also, this question may be asked by the interviewee in your Java-related interviews. So the answer to this question is Java is both Interpreted and compiled. However, it isn't clear whether Java is compiled or interpreted. It neither generates machine code after compiling a source file nor interpreted the source file to execute code instructions line by line. To answer this question, we need to understand how Java is a platform-independent language? Which means we can write Java code on a platform and can run on other platforms such as hardware operating machine, without making any changes. So, understanding how Java achieves platform independence will provide a complete answer to this question.

Java is completely portable; the same Java code will run identically on different platforms, regardless of hardware compatibility or operating systems.

The Java source code first compiled into a binary byte code using Java compiler, then this byte code runs on the JVM (Java Virtual Machine), which is a software based interpreter. So Java is considered as both interpreted and compiled.

The compiled byte code allows JVM to be small and efficient, and fast performing. Also, this byte code provides portability to the Java programming language. It allows executing this code to any JVM that is properly implemented on a machine, regardless of hardware and software components & configurations of the machine. Almost all web browsers contain JVM to execute the Java applet code.

Let's understand it with a simple Java program:

Simple Java Program and It's Working

Create a simple .java file using your favourite text editor:

Is Java Interpreted or Compiled

Put the below code in it:

And save the file.

Now, check our directory where we have saved the .java file.

Is Java Interpreted or Compiled

We can execute a Java application by following two steps.

  1. Compile the Java program
  2. Execute the application

Compile the Java application:

To compile the Java program, execute the below command:

The above command will compile the Java program and create a .class file of the Java program that contains the byte code of the Java application.

Is Java Interpreted or Compiled

We can see from the above output that after compiling the Java program, a .class file is created. This file contains the byte code of the Java program. We can execute this byte code to any of the machines implementing JVM.

Execute the Application:

To execute this application, execute the below command:

The above command will execute the byte code and perform the functionality of the program:

Is Java Interpreted or Compiled

From the above output, we have printed a simple statement.

Step by Step execution of the Java program:

Is Java Interpreted or Compiled
  1. Write Java code and save the file with .java
  2. Now, this file will be compiled using the Java compiler, which is javac.
  3. The Java Compiler will compile the Java file and create a .class file having byte code ( which is not actually a machine code, unlike the C compiler)
  4. This generated byte code is a non-executable code, and now it needs an interpreter to convert it into machine code. Here the JVM handles it.
  5. Now, JVM will execute this byte code to execute Java byte code on a machine.
  6. Now, our program will perform the functionality and gives the desired output.

Conclusion:

Java is considered as both interpreted and compiled. It uses a Java compiler (javac) and JVM ( which is actually a software-based interpreter) to execute a Java application on a machine completely.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA