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)?
Bytecode is just an intermediate language.
Or the other way round: The implementation of an intermediate language is a virtual machine.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With