89

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.)

Mat
  • 202,337
  • 40
  • 393
  • 406
naeemgik
  • 2,232
  • 4
  • 25
  • 47
  • 19
    The JVM is a virtual machine. Bytcode goes in, the observable effects of the program happen. Everything else is an implementation detail. –  Oct 06 '11 at 13:27
  • 1
    Yes, there is no "the" JVM, there are multiple implementations of the spec. – paxdiablo Oct 06 '11 at 13:32
  • @delnan, please please write that as an answer. I'd really want to upvote such answer. – aioobe Oct 06 '11 at 13:36
  • @aioobe: I won't. I don't think it answers the question asked here - it's a very useful perspective, but the question (stated clearly in the title and in the post) explicitly requests these implementation details. –  Oct 06 '11 at 13:40
  • Fair enough. I'd say it *is* an answer to the *"If neither, then what exactly is the JVM?"* part though. (As a Java Virtual Machine is more of a specification than an implementation.) – aioobe Oct 06 '11 at 13:43

7 Answers7

192

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. Its 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?

    An interpreter

  2. What about JIT compiler that exist inside the JVM?

    If you read this reply completely, you probably know it now.

  3. 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 principal reasons why java is secure)
    • Next, the Execution Engine component converts the Bytecode into executable machine code
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Testaccount
  • 2,755
  • 3
  • 22
  • 27
  • You say JVM translates Bytecode into Native Machine Code. I am not sure if term "translation" is correct here. – Koray Tugay Jan 21 '15 at 13:03
  • Highly appreciated. you have almost cleared my doubt of difference between compiling and interpretation, byte code and m/c code. your answer told me that byte code gets converted to machine code by an interpreter and compiler converts source code into bytecode. – Always_a_learner Jan 15 '16 at 11:37
  • 3
    Can you also please tell me that what is native code? Does it mean machine code? I am trying to learn this whole process of compilation and interpretation but these intermediate terms are very confusing. – Always_a_learner Jan 15 '16 at 11:41
  • 8
    So both JVM and JIT converts byte code into platform specific machine code So what's the different between them? can you explain it broadly? – Aditya Dec 01 '16 at 11:49
  • The JVM, as a black-box abstraction, is just a Java bytecode interpreter. An interpreter is just a program that executes the instructions [https://en.wikipedia.org/wiki/Interpreter_(computing) ]. (Granted, the name "interpreter" is somewhat of a misnomer.) How the JVM does so is another question - in this case, to optimize performance, the JVM uses clever designs and tricks. One of its key components is the JIT "compiler". – flow2k Jul 05 '17 at 03:28
  • 8
    'Runs/ Interprets/ translates Bytecode into Native Machine Code' is nonsense. It *either* 'runs/interprets' *or* 'translates Bytecode into Native Machine Code'. Not both at the same time. – user207421 Aug 01 '17 at 01:16
  • @EJP how do you think JVM 'runs/interprets' bytecode on any machine ? – apersiankite Jun 07 '18 at 10:58
  • Why do we need 2 steps? source code to bytecode, then bytecode to machine code? why cant we convert source code to machine code directly? – RyuCoder Jul 21 '18 at 13:05
  • 1
    @RyuCoder VM is a layer of abstraction. This way Java compiler generates the same bytecode on any platform it is compiled since it is running on the same JVM. The JVM is interpreting these instructions and optimizes only the hot sequences, the ones which are executed often. We can convert source code to machine code. GCC/Clang and others do that for many years. This is just another way of building things. For example, this way we can run .jar files compiled on one architecture (like x86) and run it on ARM since it's decoupled from the native instruction set (it's coupled to the JVM instead) – denis631 Jan 22 '19 at 13:20
  • 1
    @RyuCoder You can, that's what GCJ, Multi-OS Engine, Graal native image, and possibly more JVM implementations do. Such implementations are valid JVM implementations. This answer wrongly assumes that interpretation (possibly enhanced with JIT) is the only approach to implementing the JVM. – cubuspl42 Jun 06 '19 at 10:26
  • There was a mcq on coding test. Jvm is ------ for byte code. Options were interpreter, compiler, both and none. So what will be the correct answer? – Pran Kumar Sarkar Sep 11 '19 at 12:07
33

It is a little of both, but neither in the traditional sense.

Modern JVMs take bytecode and compile it into native code when first needed. "JIT" in this context stands for "just in time." It acts as an interpreter from the outside, but really behind the scenes it is compiling into machine code.

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

Mark Peters
  • 80,126
  • 17
  • 159
  • 190
  • 8
    @NaeemShah: I'm flattered that you liked this answer enough to copy it almost word-for-word into a blog post under your own name. And you have the right to do so, but under StackOverflow's licensing scheme, you are legally required to give attribution back to here, and you must license your blog post under the same license. See the footer of this website, which links to the following license: http://creativecommons.org/licenses/by-sa/3.0/. See also this blog post: http://blog.stackoverflow.com/2009/06/attribution-required/ – Mark Peters Nov 28 '13 at 16:44
11

Like @delnan already stated in the comment section, it's neither.

JVM is an abstract machine running Java bytecode.

JVM has several implementations:

...and many others.

Most of the others answers when talking about JVM refer either to HotSpot or some mixture of the above approaches to implementing the JVM.

cubuspl42
  • 7,833
  • 4
  • 41
  • 65
  • 1
    ART is also an interpreter: *"Android can actually run the `.dex` code directly via interpretation or Just-In-Time (JIT) compilation ..."* (https://source.android.com/devices/tech/ota/ab/ab_faqs#how-did-you-halve-the-size-of-the-system-partition-without-squashfs) – Irfan Latif Feb 29 '20 at 22:38
6

It is both. It starts by interpreting bytecode and can (should it decide it is worth it) then compile that bytecode to native machine code.

Paul Cager
  • 1,910
  • 14
  • 21
5

It's both. It can interpret bytecode, and compile it to native code.

Mat
  • 202,337
  • 40
  • 393
  • 406
0

Javac is a compiler but not a traditional compiler. A compiler typically converts source code to Machine level language for execution and that is done in a single shot i.e. entire code is taken and converted to machine level language at ONCE. (more on this below). While, JavaC converts it to Bytecode instead of machine level language.

JIT is a Java compiler but also acts as an interpreter. A typical compiler will convert all the code at once from source code to machine level language. Instead, JIT goes line by line (line by line execution is a feature of Interpreters) and converts bytecode generated by JavaC  into machine level language and executes it. JVM which has JIT in it has multiple implementations. Hotspot being one of the major ones for Java programming. Hotspot implementation makes JIT optimize the execution by converting chunks of code which are repetitive into Machine level language at once (like a compiler as mentioned above) so that they can be executed faster instead of converting each line of code 1 by 1. So, the answer is not Black and White with respect to the typical definitions of Compiler and Interpreter.

This is my understanding after reading several online answers, blogs, etc. If somebody has suggestions to improve this understanding, please feel free to suggest.

Sarth
  • 266
  • 2
  • 5
-7

JVM have both compiler and interpreter. Because the compiler compiles the code and generates bytecode. After that the interpreter converts bytecode to machine understandable code.

Example: Write and compile a program and it runs on Windows. Take the .class file to another OS (Unix) and it will run because of interpreter that converts the bytecode to machine understandable code.

The_Fox
  • 6,992
  • 2
  • 43
  • 69