This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

compilation & interpretation

 
Amit Sethi
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 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...
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic