Happy Mittal
Happy Mittal

Reputation: 3747

Difference between a Java interpreter and JVM

I have heard people saying "a JVM is necessarily a Java interpreter but a Java interpreter is not necessarily a JVM". Is that true?

I mean is there a difference between a Java interpreter and JVM?

Upvotes: 12

Views: 31063

Answers (6)

L Fields
L Fields

Reputation: 81

Simply put, a JVM interprets bytecode and a Java interpreter interprets Java. They are different because bytecode and Java are different languages.

Bytecode is a low-level language, like machine code. The bytecode is meant to be run by a program called a bytecode interpreter, also called a virtual machine. The purpose of bytecode is to be easy to interpret.

Java is a higher-level language, like C or Python. These languages can be interpreted too: you just write a program that can run their code. It doesn't have to involve bytecode. It's just that higher-level languages are harder to interpret directly.

Java is usually "interpreted" by translating the Java program into a bytecode program first. Then the Java Virtual Machine (JVM) runs the bytecode.

But you could interpret any language this way. The JVM could interpret other languages if you translated them into the right bytecode.

You can also interpret a programming language directly, without any bytecode. Some BASIC interpreters just look for BASIC instructions in the sourcecode and execute them. They don't make make a new program in a different language first. If you did the same thing for Java, it would be a Java interpreter but not a JVM.

Upvotes: 8

SARUPPYA SATPATHI
SARUPPYA SATPATHI

Reputation: 1

java virtual machine is a virtual processor and a java interpreter is java tool.thanks

Upvotes: 0

YoK
YoK

Reputation: 14505

Yes, there is a difference.

Java virtual machine:

A software "execution engine" that safely and compatibly executes the byte codes in Java class files on a microprocessor (whether in a computer or in another electronic device).

Java interpreter:

A module that alternately decodes and executes every statement in some body of code. The Java interpreter decodes and executes bytecode for the Java virtual machine.

The Java interpreter is actually a part of JVM. Virtual machine is not just executing the bytecodes, it has lot of tasks to do. That full-fledged environment is referred to as a JVM.

Check:

Upvotes: 16

ravibhagw
ravibhagw

Reputation: 1740

As I understand it...

A Java interpreter executes lines of byte code as commands to be executed. The byte code is executed.

The JVM takes the byte code and generates machine code. The byte code is compiled to machine code, and the machine code is executed.

Upvotes: 0

Kent Murra
Kent Murra

Reputation: 244

Calling a JVM a Java interpreter is incorrect. The JVM is a JIT compiler that compiles and runs byte-code. Other languages can be compiled into byte-code targeted for the JVM. Wikipedia article detailing such languages.

Upvotes: 0

Justin Ethier
Justin Ethier

Reputation: 134167

For one, code from (theoretically) any language can be compiled down to JVM bytecodes to allow execution within that environment. A Java interpreter is only able to run Java code.

Upvotes: 0

Related Questions