Interpreter in Java | Interpreter vs Compiler

Interpreter in Java is a computer program (software) that implements Java Virtual Machine (JVM) and runs Java applications (programs).

It translates (converts) bytecode to machine code (native code) line by line during runtime.

In other words, Java interpreter is a computer program that converts the high-level program into assembly language (machine language).

It is used for executing Java programs. Interpreter takes bytecode as an input and executes that code by converting it to machine code. It is recognized by “Java.exe” command.

Some of the Java interpreter command options are as follows:

1. -version: display interpreter version.
2. -verbose: display interpreter information.
3. -help: display interpreter options.

The interpreter options are case-sensitive. A screenshot of Java interpreter options is shown in the below figure.

Java Interpreter command options

Functions of Java Interpreter

Java interpreter is responsible for the following functions that are as follows:

1. The main function of interpreter is to convert the bytecode instruction to machine code line by line at runtime, without changing the sequence.

2. It performs all the activities of Java runtime system.

3. It runs application programs by reading and interpreting bytecode files.

How does Java Interpreter work?

The below figure shows how Java works on a typical computer.

Process of converting byte code into machine code

When Java program is compiled, the compiler generates an intermediate code (in the format of .class file) known as bytecode for JVM.

Java Virtual Machine exists only in the computer memory. It is not made of hardware or other physical components.

These byte codes are machine-independent and therefore can be executed on any machine. Java interpreter reads byte codes line by line and converts them into machine code for a specific machine on which Java program is running.

Therefore, an interpreter program is individually written for each type of machine. JVM uses the interpreter at runtime and then executes the code on the host machine after interacting with the operating system.

Many browsers such as Google Chrome, Netscape, etc. are popular browsers that contain the Java interpreter. This means that these are Java-enabled browsers. It is used to execute the Applet program in the browser.

Features of Interpreter

There are many features of interpreter in Java that should be kept in mind. They are as follows:

  • An interpreter is easy to use for beginners.
  • It acts as an intermediary between Java Virtual Machine and real machine.
  • Interpreter program is different for different machines.
  • It translates the source code into machine-specific code line by line at run time, without changing the sequence.
  • It does not produce an intermediate machine code.
  • Each error of every line is displayed one by one by the interpreter.
  • Interpreter executes the program at a slower speed while comparing to a compiler.
  • It spends less time on analyzing and processing the program.

Interpreter vs Compiler: How is Interpreter different than a compiler in Java?

Here, we have listed the key differences between an interpreter and a compiler. They are as follows:

1. An interpreter translates program line by line whereas, a compiler translates the entire program together.

2. Execution process of Interpreter is slower whereas, execution process of the compiler is faster.

3. Interpreter takes less compile-time whereas compiler takes more time.

4. It will not produce intermediate object code. Compiler produces intermediate object code.

5. Interpreter compiles the program until an error is found. Compiler shows all the errors once at the end of the compilation.

6. Python, PHP, Ruby, and Perl use an interpreter. Java, C++, Scala, and C use a compiler.

Hope that this tutorial has covered all the important points related to interpreter in Java and its features, functions. I hope that you will have understood the basic points of interpreter.
Thanks for reading!!!