Java is an interpreted language...why?

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.

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.

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.

Nitin Bhardwaj
Ranch Hand

Joined: Feb 02, 2007
Posts: 72

posted Feb 26, 2009 00:25:55

0

Thanks Amitabh for trying the question (edited my previous message because still there was more info to be added to it ;) )

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

posted Feb 26, 2009 01:03:52

0

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
Rancher

Joined: Mar 22, 2005
Posts: 42954

73

posted Feb 26, 2009 01:08:45

0

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.