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:
Put the below code in it:
And save the file.
Now, check our directory where we have saved the .java file.
We can execute a Java application by following two steps.
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.
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:
From the above output, we have printed a simple statement.
Step by Step execution of the Java program:
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.
Next TopicJava Big Data Frameworks