开发者

Difference between a Java interpreter and JVM

I have heard people saying "a JVM开发者_StackOverflow 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?


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:

  • Java Virtual Machine

  • Java SE HotSpot at a Glance


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.


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.


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜