aspose file tools*
The moose likes Beginning Java and the fly likes What is Java Interpreter or Compiler? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is Java Interpreter or Compiler?" Watch "What is Java Interpreter or Compiler?" New topic
Author

What is Java Interpreter or Compiler?

Chandra Bairi
Ranch Hand

Joined: Sep 12, 2003
Posts: 152
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
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
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
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
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
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
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

Still helpful, even after 10 years .......


OCPJP 6 86%
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Slight change: you can assume nowadays that JVMs are JIT until proven otherwise. They run much faster than they used to.
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

Sherrif, i have a doubt, in below link

http://docs.oracle.com/cd/E15289_01/doc.40/e15058/underst_jit.htm

this example says that jit changes method call to returned values.

But what if method does not return anything ??
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
In which case it would not inline anything.
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

Campbell Ritchie wrote:In which case it would not inline anything.


Ah, means JIT comes into play if and only if method returns somthing .
am i correct Sherrif ??
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
No. It means it cannot inline the return value from a method which hasn’t got a return value.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8419
    
  23

Nikhil Sagar wrote:Ah, means JIT comes into play if and only if method returns somthing .
am i correct Sherrif ??

Given that you've revived a 10-year-old thread, maybe the more relevant question is: Why do you want to know this?

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

Winston Gutkowski wrote:
Nikhil Sagar wrote:Ah, means JIT comes into play if and only if method returns somthing .
am i correct Sherrif ??

Given that you've revived a 10-year-old thread, maybe the more relevant question is: Why do you want to know this?

Winston


Just for knowledge.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is Java Interpreter or Compiler?