File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes compilation & interpretation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "compilation & interpretation" Watch "compilation & interpretation" New topic

compilation & interpretation

Amit Sethi
Ranch Hand

Joined: May 30, 2006
Posts: 45
Q. What is actually happening behind the scene when we compile the .java file & when we interpret the .class file ? can any one pls give me a full step wise summery of what is going on behind the scene's during these two processes.
Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 389
Hi Amit,

In java running & executing a program is two step process. The first step is compilation & then it is interpretation.

After we write a program (source code with ext , we write javac <>. This step is the compilation part. The compilation basically checks for language errors or syntax errors. There are certain rules that java follows, for ex, each statement should end with semicolon, the syntax of for loop etc. When a program is successfully compiled, the compiler says something like ... okay this source code is according to the java rules. When a program is compiled, it creates a .class file or more precisely byte code

The second part is the interpretation part. Now this bytecode is executable by any machine that has a JRE or JVM. The byte code, is a code that is independent of all the systems. So any device/pc capable of executing java can execute the above byte code. The JVM is basically the virtual machine within an physical machine. The JVM basically interprets the byte code / class file to execute the program.

The above two parts is quite a power ful due to the fact that the above process makes java platform independent. It is due to the fact that the java code is execuited by the virtual machine rather than the phycial machine.

I am not sure whether how successful i was in solving your query...

SCJP 1.4, SCWCD 1.4, SCBCD 1.5, TOGAF 9, Comptia Cloud+
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Nicely put.

"Platform independent" is a bit vague and misleading. It's Sun's term so blame them. I'd rather say byte code is portable to any compatible JVM. So you can compile source to byte code once and run that byte code on any platform that has a JVM. The byte code might do things that don't work on that platform, like trying to exec() Internet Explorer on UNIX, but that's the programmer's fault, not Java's.

There is one more step just to make things confusing. Some or most JVMs have "hot spot" technology to compile the byte code again, this time creating machine code for the hardware and OS where it is running. After a little overhead for compiling this machine code can run significantly faster than the byte code interpreter.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: compilation & interpretation
jQuery in Action, 3rd edition