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
Thanks,
Shekar
Dave Vick
Ranch Hand
Joined: May 10, 2001
Posts: 3244
posted
0
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 ]
Dave
Chandra Bairi
Ranch Hand
Joined: Sep 12, 2003
Posts: 152
posted
0
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 .
Alaa Abutabaq
Greenhorn
Joined: Jan 15, 2002
Posts: 18
posted
0
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
Michael Fitzmaurice
Ranch Hand
Joined: Aug 22, 2001
Posts: 168
posted
0
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:
the JVM (interpreter) knows what needs to be done in say, a Windows environment, to make sure that the intent of the instruction is honoured (in this case that means talking to the OS and asking it to print the "Hello World!" text to the console). 'What needs to be done' for Linux is different to what needs to be done on Windows. It is therefore the JVM that is Java's interpreter. Don't worry too much about JIT compilation for now, this might confuse you (it is not the same as source code compilation, rather it is something that happens whilst a Java program is running [being interpreted], in an attempt to make it run more efficiently). It is something the JVM does for you. So, if somebody asks you whether Java is a compiled language or an interpreted language, the answer is that it is in fact both. Perhaps it would be useful for you to read a quick explanation of what a traditional interpreted language is, what most people understand by a compiled language, and how Java fits into these definitions: Compiled Vs Interpreted Examples of interpreted languages: JavaScript, Perl, Python Examples of compiled languages: C, C++ Hope this helps Michael
"One good thing about music - when it hits, you feel no pain" <P>Bob Marley
Dave Vick
Ranch Hand
Joined: May 10, 2001
Posts: 3244
posted
0
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