Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the JVM a compiler or an interpreter?

Tags:

java

jvm

I have a very basic question about JVM: is it a compiler or an interpreter?

If it is an interpreter, then what about JIT compiler that exist inside the JVM?
If neither, then what exactly is the JVM? (I dont want the basic definition of jVM of converting byte code to machine specific code etc.)

like image 839
naeemgik Avatar asked Oct 06 '11 13:10

naeemgik


People also ask

Is compiler a part of JVM?

No, javac isn't part of the JVM itself.

Why is an interpreter known as JVM?

Java Virtual Machine takes Bytecode as input and converts it into Machine Code one line at a time. This Bytecode can be generated by compiling source code written in any JVM language like Scala, Kotlin, etc not just Java. Hence, Java interpreter is called Java Virtual Machine.

Is Java compiler and interpreter?

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.

What is difference between JVM and interpreter?

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.


1 Answers

First, let's have a clear idea of the following terms

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, Let's find answers to your questions..

1)JVM: is it a compiler or an interpreter? -- Ans: Interpreter

2)what about JIT compiler that exist inside the JVM? -- Ans: If you read this reply completly, you probably know it now

3)what exactly is the JVM? -- Ans:

  • 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

Hope this helped you..

like image 198
Testaccount Avatar answered Sep 21 '22 21:09

Testaccount