This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
What exaclty is java is it an interpreter or compiler? I have been told by my colleague that there are two phases to executing a program. compiling a source code java file to class file and the the jvm transforms the class file to byte code can anyone tell me the process of executing a file. Thanks in advance
Chandra The entire process would be something like this: you write your code and save it as a .java file. The compiler takes your .java file and compiles it into a .class file (the .class file contains Java byte code). The interpreter comes in when your program is run. The JVM (or interpreter) takes your .class file and interprets it. Keep in mind that more and more frequently as JIT becomes more prevalent it is code is not just interpreted anymore it is actually compiled at run time into native machine code. Each platform has a JVM written specifically for it so thatt he JVM for say a Macintosh knows exactly how to interpret the byte codes produced by the compiler (the .class file) and make them work on a mac, same for a Windows JVM. Hope that that helps. [ October 13, 2003: Message edited by: Dave Vick ]
Joined: Sep 12, 2003
Thanks Dave, I'll just explain kindly let me know if that is correct. 1)Firstly we write a java file with .java extension. 2)That is compiled to .class file which contains Java byte code. 3)This .class file is interpreted by JVM i.e it interprets the byte code to native OS code. 4) The code is executed while interpreting by the JVM. When asked about java what should I exaclty say is it a compiler or interpreter Kindly correct me at any step and please dont mind i have no idea about JIT. Thanks a lot .
Hi First , Java is Object Oriented programming language. Java is the language itself . The compiler is the tool that used for create a byte code file (.class), the the Java Virual Machine can understand, according to a text based file(.java) written in Java code. JVM is where the Java byte code file(.class) can be run . Second, JIT is one of the java compilers (Just-In-Time compiler). Third, "This .class file is interpreted by JVM i.e it interprets the byte code to native OS code." This is not accurate . The .class file is interpreted by JVM that means JVM reads the .class file and understand it.So when u run a java program it understands what to do. Last, I hope that I was helpful for u and sorry if it was not clear. Thanx Ala'a
Hi Chandra You have pretty much got the idea, but I get the feeling the concept of the JVM interpreting the byte code (.class files) is still not completely clear to you. The reason that you need to use a JVM (Java Virtual Machine) that is appropriate to your platform (there is one for Windows, one for Linux, one for OS X, etc.) is that the JVM is acting as the middleman between the byte code and platform-specific OS calls. So, for example, when you have a line of Java code that looks like:
"One good thing about music - when it hits, you feel no pain" <P>Bob Marley
Joined: May 10, 2001
Chandra You've pretty much got it. Like Alaa said though the JVM does not neccesarily turn the byte code into native code unless it uses a Just-In-Time (JIT) compiler. And even then it might only be a small part of the code that gets compiled to native code. Basically the interpreters job is to read the Java byte code, then decide how to make the operating system do what it is that the byte code says. When asked which is the interpreter and which is the compiler, the simplest answer is that the compiler takes the .java text file and turns it into java byte code in a .class file. The interpreter, or JVM, is what reads the byte code, the .class file, and causes it to execute. Hope that helps