Reputation: 3674
I am trying to understand the real advantage of implementing java as an abstract or virtual machine or in other words the advantage of compiling a language into a language for an abstract machine. As far as platform independence is concerned I was thinking of the following two alternative implementations:
just having an interpreter which translates java directly into machine code of the machine it is running on and having multiple implementations of such an interpreter for different type s of machine.
the first option is not efficient in space so how about compiling the source code to an intermediate language which is not a language for an abstract machine but just some language which can be interpreted to machine code and then having multiple implementations of such interpreters.
If performance is not considered how does having an abstract machine compare with these options. In other words what if java byte code is not a language for a virtual machine but just some intermediate language.What features and benefits would be lost (except for performance)?
Upvotes: 7
Views: 3605
Reputation: 3078
All variants are actually in use in practice, it is all about choosing appropriate compromises.
for Java - convenient for many platform distribution, slower startup times:
for JavaScript - the most convenient for distribution / a lot of work to be done to make it fast):
for .NET - AOT has all advantages of VM language while keeping startup time fast, but mostly locked to one target system type:
this is just what is mostly being used, but you can compile javascript to bytecode, precompile java to machine code, you can even compile Java to javascript like GWT does, etc. (only there is a lot to do to make it usable)
Upvotes: -1
Reputation: 3260
Bytecode is just an intermediate language.
Or the other way round: The implementation of an intermediate language is a virtual machine.
Upvotes: 5
Reputation: 814
What you describe is essentially what Java and the JVM do currently. Java is compiled to something called bytecode, which is an intermediate language (that happens to look an awful lot like assembly for a stack based machine). The JVM then interprets this code, translating parts of it to machine code on the fly in a process called Just In Time (JIT) compilation. The JVM does other things (like manage concurrency and memory structure/management) which aid in portability.
Upvotes: 2
Reputation: 111219
If Java was translated directly to machine code as it is executed you would lose the type safety features that the compiler provides. The fact that the compiler reports no errors guarantees that certain types of errors cannot occur in runtime; if you remove the compiler phase you would see these errors in runtime.
On the other hand, Java bytecode is an intermediate language, even if it's a little higher level than others. JVMs execute it partly by interpreting and partly by compiling to machine code.
Upvotes: 2