This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.
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 filename.java) , we write javac <filename.java>. 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...
"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