0

My understanding is about how java code is executed by jvm is

  1. Developer writes the .java file . Computer internally stores it in bits like everything else
  2. Now Java compiler compiles the bits into byte code(or we can say instructions specific to JVM) which JVM can interpret/execute easily. Right ?
  3. Now when we say JVM executes the byte code. I believe it means translating the byte code to machine instructions which CPU can execute. For example :- JVM translate the byte code to machine instruction specific to processor and OS(system calls). Is that correct ?
scott miles
  • 1,511
  • 2
  • 21
  • 36
  • The JIT performs that step. There are platforms without JIT (and there are CPUs that execute [byte code directly](https://en.wikipedia.org/wiki/Jazelle)). – Elliott Frisch Feb 16 '17 at 01:23
  • JIT is the pasrt oF oracle provided JRE right ? – scott miles Feb 16 '17 at 01:28
  • Sorry for closing, but it isn't new. Adding some information: Ad 3: A typical JVM starts by interpreting the bytecode, i.e., looking at it, deciding what to do and doing it. That's terribly slow and soon the [JIT](https://en.wikipedia.org/wiki/Just-in-time_compilation) comes and translates it into machine code. There are actually two such compilers in the [Hotspot JVM](http://openjdk.java.net/groups/hotspot/docs/HotSpotGlossary.html), C1 and C2. +++ Android [used to](https://source.android.com/devices/tech/dalvik/#AOT_compilation) work similarly. – maaartinus Feb 16 '17 at 01:30
  • @maaartinus even before JIT came in to picture. It was JVM which was converting byte code to machine code for cpu. So why JIT was required ? – scott miles Feb 16 '17 at 01:34
  • @scottmiles: The interpreter is a piece of program working like: 1. Get an instruction, 2. nice, it's an [iadd](https://en.wikipedia.org/wiki/Java_bytecode_instruction_listings), 3. so get the operands, 4. add them, and 5. store the result. It may take a few cycles. +++ When compiled, the CPU sees some [ADD](http://x86.renejeschke.de/html/file_module_x86_id_5.html) machine code and does about the same as the interpreter, but it takes just a single cycle and it's [superscalar](http://stackoverflow.com/a/30824781/581205). +++ My figures are just an example, but a speed ratio of 10 is realistic. – maaartinus Feb 16 '17 at 01:41
  • @maaartinus sorry i am bit confused. when you say `When compiled, the CPU sees some ADD machine code and does about the same as the interpreter` do you mean interpreter(jvm in this post) can execute instruction(byte code ) directly without converting it to machine language for CPU. The main difference is if it is done through CPU it will be faster. So in a way JVM is also virtual processor. is that right now ? – scott miles Feb 16 '17 at 02:01
  • 1
    The Java Runtime Environment consists of a [bytecode interpreter](http://stackoverflow.com/questions/6709440/java-bytecode-interpreter) and one (or more, or fewer) [JIT](https://en.wikipedia.org/wiki/Just-in-time_compilation) compilers. – Elliott Frisch Feb 16 '17 at 02:04
  • @maaartinus can you please have a look at mine last comment ? – scott miles Feb 16 '17 at 04:24
  • @scottmiles I has already and upvoted Elliots' last comment instead of writing my own. And yes, any interpreter is a sort of virtual CPU capable of executing bytecodes or whatever directly. – maaartinus Feb 16 '17 at 21:05
  • Thank you maaartinus and Elliott – scott miles Feb 17 '17 at 05:07

0 Answers0