File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Java is an interpreted language...why? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java is an interpreted language...why?" Watch "Java is an interpreted language...why?" New topic
Author

Java is an interpreted language...why?

Nitin Bhardwaj
Ranch Hand

Joined: Feb 02, 2007
Posts: 72

Dear Friends,

This one question has troubled me a lot:
Why Java is an interpreted language? A Java source file is also compiled and a C++ source code file is also compiled. A .class file in Java is interpreted by the JVM...what does it mean? What is the basic difference between a compiled language and a interpreted language?
Request you to please throw some light on this topic to clear the doubts.

Thanks & Regards,
Nitin
amitabh mehra
Ranch Hand

Joined: Dec 05, 2006
Posts: 98
JVM mostly uses interpreter to convert bytecode to machine dependent code. There are a few cases where JIT compilers (and also ahead of time compilers )are used to do this task.
The bytecode (i.e., using javac compiler to convert the source code to byte code) is machine independent. Java follows "compile once, run anywhere". So now this bytecode can be run on any machine. To convert this bytecode to machine understandable code and execute it , interpreter (or compiler as explained earlier) is used.

One of the reasons of not using the compiler at bytecode->machine code conversion stage is performance. JVM requires some run time optimization. So compiling the bytecode will not help much on this front.

Just my understanding.. If I have missed out something, or have stated wrongly, please enlighten me

For "difference between a compiled language and a interpreted language", a simple google search will give you the differences.

Thanks
Amitabh
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39549
    
  27
One of the reasons of not using the compiler at bytecode->machine code conversion stage is performance. JVM requires some run time optimization. So compiling the bytecode will not help much on this front.

That's not quite correct. The JIT is a compiler, and it's used precisely to boost performance. If the JVM determines that a method is used frequently, it will most likely be compiled to native code by the JIT. But the byte code is still available, and the JVM is capable of throwing out the generated native code, and recompile the bytecode in a different way if that's deemed more efficient.

Not a beginner's topic, so I'll move it to the intermediate forum.


Ping & DNS - updated with new look and Ping home screen widget
Nitin Bhardwaj
Ranch Hand

Joined: Feb 02, 2007
Posts: 72
Thanks Amitabh for trying the question
(edited my previous message because still there was more info to be added to it ;) )

Best Regards,
Nitin
Dhruva Mistry
Ranch Hand

Joined: Nov 21, 2008
Posts: 67

Nitin Bhardwaj wrote:
Dear Friends,

This one question has troubled me a lot:
Why Java is an interpreted language? A Java source file is also compiled and a C++ source code file is also compiled. A .class file in Java is interpreted by the JVM...what does it mean? What is the basic difference between a compiled language and a interpreted language?
Request you to please throw some light on this topic to clear the doubts.

Thanks & Regards,
Nitin


java file gets compiled and is converted to semi-binary format ie bytecode
it's made to provide platform independency
as we can run bytecode on different machines but with jvm and it will give you same output
whereas in case of C++, compiler directly converts source code to machine code ie binary code

after getting java file's bytecode, while running on machine, jvm is used and JIT is a compiler ie a part of jvm that converts bycode to machine code ie binary code by interpreting the bytecode file's all code lines one by one


Dhruva
amitabh mehra
Ranch Hand

Joined: Dec 05, 2006
Posts: 98
Ulf Dittmer wrote:
That's not quite correct. The JIT is a compiler, and it's used precisely to boost performance. If the JVM determines that a method is used frequently, it will most likely be compiled to native code by the JIT. But the byte code is still available, and the JVM is capable of throwing out the generated native code, and recompile the bytecode in a different way if that's deemed more efficient.


So is there any specific reason why interpreter is used and not compiler? If its enhancing performance (barring the first time where it will do the optimization and cache it), then it should be the one being used instead of interpreter. or does using compiler at this stage has some drawbacks?

And the case I was talking about was a general case wherein these optimization and caching will not be of great importance. In a large application, what should be done - compile or interpret the bytecode.... compiling the bytecode might take a lot of time...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39549
    
  27
Compiling a method from bytecode to native code takes time. The JVM will only do that if a method is called frequently, and/or takes a certain amount of time to execute. So it depends on the results of the runtime profiling the JVM does. If all code was compiled at application startup, you might have to wait a while until something happens.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java is an interpreted language...why?
 
Similar Threads
interview question
compiler some times does NOT report all errors
why java interprets?
JVM
java compiled or interpreted ?