Skip to main content

how is JVM interpreter really better/cheaper than a compiler made for C/C++

2 replies [Last post]
Joined: 2009-10-21

I have just started studying java and this question has been bugging me .
I know C/C++ is present for almost every platform/machine . Java creates a platform free programs by having a compilation done to intermediate bytecode and then platform dependent interpretation done JVM , then
How is JVM interpreter plus compiler cheaper than a compiler made for C/C++.
Since lexical analyzer, syntax analyzer and semantic analyzer would be same if we make the compiler for different platforms only thing that would change is optimization modules (platform specific optimization) . Since C/C++ compilers does non platform specific optimization on it's intermediate abstract tree representation , only thing left to change is platform specific optimization modules in the compiler .But i think JVM would also be doing platform specific optimization to improve efficiency.
And incase JVM is not doing any platform specific optimization then isn't the final
code quite less efficient than native platform compilation .

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2009-12-30

While the previously mentioned article is full of some valid criticisms of the C programming language, I think it really didn't go to the meat of your question which is, "why bother with a virtual machine when you go directly to machine code".

The answer is portability.

It is true that C is relatively portable. I can write it on a Windows system, then compile the same code on a Mac. And if it is fairly simple code it will, in fact, work in both places.

But when I distribute that code on a CD I must include the Mac OSX version, the Windows version, the Linux Version, the Solaris version, and well... you get the picture. One idea of Java was that you write/compile the code once, and then it works on every copy of the Virtual machine because those virtual machines are all just different implementations of the same computer. You could say that the JVM is an emulator of a fictional box and that the definition of a "good" emulator is how well it goes about running every possible sequence of instructions in exactly the same way as every other emulator.

Another issue with a "compile it once for every platform" idea is that not all platforms are the same. Consider the graphic user interface of two popular platforms. How do I put up a window on the screen. What are the properties of that window? The GUI created by Apple (OS X) is going to have some different answers those questions than the one created by Microsoft (Windows) which will in turn be different than the one created by Unix community (e.g. gnome). Most will have similar features such as checkboxes and edit boxes, but they may implented and called in a different manner. Maybe things like the clip board work differently. Trying to make every program work on every platform usually involves re-writing lots of code.

This is another place that Java Excels. Sure, the GUI of the virtual machine is somewhat primative compared to the full features you get if you program directly for the OS. But you are assured that if the feature exists on the platform on which you built the code, than that same feature will exist on every other JVM as well.

Of course, that is the theory of it. I am sure that there many examples where the code doesn't work quite right. Where the notion of "write once, works everywhere" just doesn't apply. But that is the objective of something like Java.

Joined: 2007-05-26