Chiron
Chiron

Reputation: 20245

Java code and JIT compilation

Java code is compiled to bytecode which it is portable across many platforms. But Java also is JIT compiled which happens on the fly.

Does this mean Java is compile twice? first by us to produce the bytecode and the second by the JVM? Thanks.

Upvotes: 3

Views: 1892

Answers (5)

mathk
mathk

Reputation: 8133

Short answer: Yes kind of.

The longest one: That is 2 different things. The first compilation is from the source code to the bytecode often call the intermediate representation (IR) in the compilation field.

Then the VM take the bytecode and translate it back to the native code on which platform it is installed.

This is 2 completely different kind of compilation. The second is not even quite a compilation since there is no syntax checker scope analyzer... Well there is some check but not the same kind of check that you have in a compiler.

Upvotes: 0

Anindya Chatterjee
Anindya Chatterjee

Reputation: 5964

The mechanism is

Java -> Byte Code (compiled By java compiler)

ByteCode -> native code (interpreted by JVM)

Upvotes: 1

bakkal
bakkal

Reputation: 55448

Does this mean Java is compile twice? first by us to produce the bytecode and the second by the JVM? Thanks.

You could say that, once using information available in the source code (compiler), then the other in runtime (JVM/JIT) when information about the specific hardware is available, along with some profiling to decide what gets to be JIT-compiled or not.

Upvotes: 2

vanza
vanza

Reputation: 9903

Your code may be compiled from bytecode into native code by the JVM if it's "hot enough"; and it may be so compiled several times, with the old version being discarded, depending on the runtime characteristics of your program.

The JIT is a complicated beast; in fact the Sun JVM has two JITs (-client and -server) that behave differently from each other, and some implementations even support both JITs running together (so you may have interpreted bytecode running alongside code compiled by two different JITs in your application).

I recommend you read more about Hotspot (the most common JIT since it's the Sun one) if you're really interested in this subject. You can start at Sun's page.

Upvotes: 7

TheLQ
TheLQ

Reputation: 15008

TMK, when you compile you are compiling for the JVM platform. Then when you run the app on the JVM on any machine, certain parts of code that are used frequently get compiled into code native to that machine for optimization.

So in short: Yes, but for a very good reason

Upvotes: 7

Related Questions