4

i know that java programs are first compiled and a bytecode is generated which is platform independent. But my question is why is this bytecode interpreted in the next stage and not compiled even though compilation is faster than interpretation in general??

3
  • you can consider the bytecode assembler for some unknown processor. The processor is then emulated by combination of interpretation and (heavily) optimized code generated for the underlying hardware the bytecode is being run onto. Commented Jul 15, 2011 at 15:36
  • @isha - the parts about JIT compilation in A JVM Does That by Cliff Click may interest you. Commented Jul 16, 2011 at 11:05
  • @McDowell, I reckon it's a good watch although it could be a bit too advanced for the general public. Commented Jul 16, 2011 at 13:05

5 Answers 5

3

You answered your own question. Byte code is platform independent. If the compiled code was executed then it would not work on every OS. This is what C does and it is why you have to have one version for every OS.

As others have suggested, the JVM does actually compile the code using JIT. It is just not saved anywhere. Here is a nice quote to sum it up

In a bytecode-compiled system, source code is translated to an intermediate representation known as bytecode. Bytecode is not the machine code for any particular computer, and may be portable among computer architectures. The bytecode may then be interpreted by, or run on, a virtual machine. The JIT compiler reads the bytecodes in many sections (or in full rarely) and compiles them interactively into machine language so the program can run faster

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

4 Comments

In fact some JVM do compile the bytecode for execution; it just is not stored anywhere: JIT (Just In Time) compilation
Good point! I was under the impression that JIT compiles only selected lines of code that are executed a lot. Maybe it compiles more than that.
AFAIK this depends of the implementation of the VM. V.g, Sun's VM combined both interpretation and JIT when in 'server' mode. Do not know how other JVM work, anyway. I only wanted to point out that the idea of compiling the code has not been overlooked by Java, even if it sticks to the advantages of bytecode and VM.
Thanks everyone!!the disscussion made me clear about compilation process and jit
2

The Java bytecode normally is compiled via Just-In-Time (JIT) compilation.

So you still end up with fully compiled native code being executed, the only difference is that this native code is generated by the JVM at runtime, rather than being statically generated at the time the source code is compiled (as would happen with C/C++).

This gives Java two big advantages:

  • By delaying the compilation until runtime, the bytecode remains fully portable across platforms
  • In some cases the JIT compiler can actually generate more optimised native code because it is able to exploit statistics gathered by examining the execution parths of the code at runtime.

The downside, of course, is that the JIT compiler needs to do it's work at application start-up, which explains why JVM applications can have a slightly long start-up time compared to natively compiled apps.

Comments

1

The basic premise of your question is not true. Most modern Java virtual machines do compile frequently-executed parts of the code into native machine code.

This is known as just-in-time compilation, or JIT for short.

A pretty good introduction to relevant Sun's (now Oracle's) technology can be found here.

Comments

0

The JVM uses Just in time compilation http://en.wikipedia.org/wiki/Just-in-time_compilation, so it's much faster than pure interpretation.

Comments

0

Byte code is platform independent. Once compiled into bytecode, it could run on any system.

As it says on Wikipedia,

Just-in-time compilation (JIT), also known as dynamic translation, is a method to improve the runtime performance of computer programs.

I recommend you to read this article. Its gives the basic working of JIT compiler for Java VM.

JIT compilers alter the role of the VM a little by directly compiling Java bytecode into native platform code, thereby relieving the VM of its need to manually call underlying native system services. The purpose of JIT compilers, however, isn't to allow the VM to relax. By compiling bytecodes into native code, execution speed can be greatly improved because the native code can be executed directly on the underlying platform.

When JIT compiler is installed, instead of the VM calling the underlying native operating system, it calls the JIT compiler. The JIT compiler in turn generates native code that can be passed on to the native operating system for execution. The primary benefit of this arrangement is that the JIT compiler is completely transparent to everything except the VM.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.