Java Code Samples

Java – Interpreted or Compiled ?

Posted in Java, Miscellaneous by Cristian on January 29, 2014

Well the answer is not so simple, but we will have one by the end of this short article :)

We all know that the source code of a Java file is ‘compiled’ into a .class file.

I put compiled in quotes because the .class file that results is not actually the machine code executed by the machine

The .class (JVM byte code) file might be seen as an intermediary language that might be even modified before is interpreted , compiled and executed.

This happens very often in an JEE environment and it is called weaving.Weaving is a technique of manipulating the byte-code of compiled Java classes.

After we have the final version (might be directly and not necessarily weaved),the byte-code is interpreted and the code is executed .

So where the code is then actually compiled !?

Well an interpreter can generate machine code to get a job done faster if this is important.

A modern VM will use all kind of techniques to do it faster and efficient. For example, for each instruction in the intermediate language the VM could ask which is going to be quicker: to call a routine or to generate machine code to get the job done. This approach is often called Just-In-Time or JIT compilation. It is usually explained as the VM compiling the intermediate language just before it is run, but this isn’t really a good way to think of it.

So the JIT is a sort of mixture of interpreting the code and compiling the code according to what makes best use of the real machine.

To respond to the initial question : it is both !

A diagram that describe the above:

JIT

About these ads
Tagged with: , ,

3 Responses

Subscribe to comments with RSS.

  1. Paul Topping said, on February 1, 2014 at 6:24 pm

    Although your explanation is interesting, it might mislead newbies in its use of terminology. Virtually every language has to be compiled into some other form before execution. Very few languages are executed directly from source as that results in very bad execution performance. Java is an interpreted language because its compiler generates intermediate code (byte code or some other form) rather than machine code and, therefor, requires an interpreter to execute programs written in it.

    • Tony Weston said, on February 2, 2014 at 9:08 am

      Even things which are traditionally compiled, like ‘C’ programs, creating an executable. This executable wont run directly on a CPU. It has to be directly linked with Win32 libraries, etc. otherwise the identical executable would run on both mac and pc, as both platforms use intel chips, and as we know they don’t!

      BUT….even putting this asside the, the x86 instructions are not run directly on the intel chip. They are compiled to internal microcode, in order to be executed on the internal RISC core. Intel chips, you could argue are interpreters of X86 machine code.

      • Paul Topping said, on February 2, 2014 at 6:31 pm

        Sure, this is all true but what I am talking about is standard use of terminology, You can always play games with words. For example, every CPU is an interpreter regardless of its internal architecture but you would use that term that way if you want to be understood.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: