1

After hours of research I haven't found a concrete answer for my question and I'm going maddd!:

The steps from editing to execution:

1 . (Compilation step) After writing the source code, i compile the program. In this step it is converted into bytecode. A java.class file (the bytecode) is generated.

2 .(Execution step) Now i execute the program.

  1. (Interpretation step) When I do this, the JVM interprets the bytecode into machine code. So I understand that the machine code is only generated after execution!??

Now the steps are: code-->bytecode-->execution-->machinecode

All these steps are hardware- and software-independent. Am i right?

This is called JIT (just in time compilation), so that when I execute the program the bytecode is compiled into machinecode, and only then. So why is this step called interpretation?

I'm thanking you in advance for your answers!

5
  • How is this related to programming? Commented Nov 4, 2016 at 16:18
  • @ItamarGreen actually it is. Java programmer should understand how JVM works. Commented Nov 4, 2016 at 16:18
  • @Divers notice I was asking how. I'm not saying it isn't and you're quite right. However, I still think it doesn't entirely belong here. Commented Nov 4, 2016 at 16:20
  • "So why is this step called interpretation" : actually, there is a distinction between interpretation and JIT compilation. Using for exemple Hotspot (the JVM in OpenJDK and Oracle JDK), the code is first executed with an interpreter; then, as the programm runs, so-called "hot spots" (pieces of codes which have been executed many times) get JIT compiled to improve performance. The same piece of code can also be JIT compiled several times over, as Hotspot tries to use better compiler optimization. Commented Nov 4, 2016 at 16:36
  • THANKS again to all of you. Its great to see taht so many people are willing to help. Initially (i started java coding around a week ago) i just wanted to consider all the steps from writing code to executing and running. So that i am 100% right: code-->bytecode-->execution-->machinecode is the right order of steps? Commented Nov 4, 2016 at 16:44

2 Answers 2

5

In short because JVM doesn't have to have JIT. It can interpret the bytecode instead of compiling it. Of course an interpret-only JVM would be slow, but the JIT part is just an extra feature to improve performance, not a required property of a Java Virtual Machine. The -Xint command line parameter can be used to run a java program in interpret-only mode.

The reason it's compiled to bytecode and not machine code is to get the platform independence. Bytecode is platform independent, so the same code can run on any platform (as long as there's the JVM to interpret it). If it were compiled into machine code, it would be operating system and processor architecture dependent.

Sign up to request clarification or add additional context in comments.

2 Comments

I understand. However, more important is my question, why we first execute the program and then do the byte to machine code transformation?
Thank you. I begin to understand. So i was right that the execution of the program comes before the machine code interpretation - or compilation, as you've said me it is both possible. I'm a complete beginner and thank you very much.
3
  1. (Interpretating step) When i do this, the JVM interprets the bytecode into machine code. So i understand that the machine code is only generated after execution!??

Not exactly, and no. A JVM operating strictly as a bytecode interpreter does not transform bytecode into machine code and then execute that. The machine code executed by such a JVM is (comprised by) the pre-existing machine code of the JVM itself. The byte code is used to provide some of the data on which to operate and to direct which of the JVM's machine code is executed.

Now the steps are: code-->bytecode-->execution-->machinecode

All these steps are hardware- and software-independent. Am i right?

No, not at all. The particulars of the Java code --> bytecode transformation are somewhat dependent on which Java compiler (software) you use. The Java virtual machine you use must be specific to the hardware on which it runs, and it is itself a piece of software. Moreover, the operating environment is influenced by a lot of other software.

Java hardware independence, such as it is, means that a Java program (bytecode) will behave consistently on any hardware, but the details of how that consistent behavior is provided on any given machine are all kinds of hardware- and software-dependent.

This is called JIT(just in time compilation), so that when I execute the program the bytecode is compiled into machinecode, and only then. But why is this step called interpretating?

JIT is something else, and a JVM that performs JIT (as in fact most do) is not strictly an interpreter. Most such JVMs run some bytecode in an interpretative manner as described above, but compile some bytecode to native (machine) code, and run that machine code directly when subsequently needed. The latter manner of execution generally isn't called "interpreting".

2 Comments

I thank you for your long answer. I start to understand. However, was i right that the JVM only begins to operate(e.g interpret or compile, how you want) after the execution?
@Mike, I'm having trouble seeing how those terms could be defined to make your proposition sensible. The operation of the JVM is its execution, and that wholly comprises the semantics of the program(s) running in it. Or in terms of bytecode, all the plausible senses of "executing" bytecode that I can see involving either interpreting the bytecode or JIT-compiling it to machine code before or as part of executing it. I see no reasonable sense of the terms that would yield bytecode executing before being either interpreted or compiled to machine code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.