Is the JVM a compiler or an interpreter?

Few days ago I was studying about JVM functionality and structure and got some confusion that what exactly JVM is? Compiler, interpreter or neither? If it is an interpreter, then what about JIT compiler that exist inside the JVM? 

The basic definition of JVM “converting byte code into machine specific code” was also not reflecting its behavior and nature to me.

A JAVA Program intended to run on a JVM must be compiled into byte code, a standardized portable binary format which typically comes in the form of .class or .jar file format.

After reading from different sources I came into point that JVM is little bit of both, but neither in traditional sense.

As modern JVMs take byte code and compile it into native code when first needed. "JIT" which stands for "Just In Time." It acts as an interpreter from the outside, but in real behind the scenes it is compiling into machine code.

The JVM should not be confused with the Java compiler, as compiler compiles source code into byte code. So it is not useful to consider JVM "a compiler" but rather to know that in the background it does do some compilation for byte code into native machine specific code.



A More Clear Description of JVM will be as:

Javac is Java Compiler -- Compiles your Java code into Bytecode

JVM is Java Virtual Machine -- Runs/ Interprets/ translates Bytecode into Native Machine Code

JIT is Just In Time Compiler -- Compiles the given bytecode instruction sequence to machine code at runtime before executing it natively. It's main purpose is to do heavy optimizations in performance.

So now, Over all definition is:

JVM: So it is an Interpreter

what exactly is the JVM?

JVM is a virtual platform that resides on your RAM
Its component, Class loader loads the .class file into the RAM
The Byte code Verifier component in JVM checks if there are any access restriction violations in your code. (This is one of the principle reasons why java is secure)

Next, the Execution Engine component converts the Bytecode into executable machine code

JAVA Compilation

If you want to read more about JVM memory management, you may refer to this

4 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Thank you because you have been willing to share information with us. we will always appreciate all you have done here because I know you are very concerned with our. private asl

    ReplyDelete
  3. This blog post has almost verbatim copies of a Stack Overflow post by Mark Peters. You are legally required to give attribution back to him and license your article under the same terms. See https://stackoverflow.com/a/7674891/200987

    ReplyDelete