Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

abstract machine concept of jvm

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)?

like image 328
vjain27 Avatar asked May 09 '12 06:05

vjain27


3 Answers

Bytecode is just an intermediate language.

Or the other way round: The implementation of an intermediate language is a virtual machine.

like image 187
michael667 Avatar answered Sep 20 '22 08:09

michael667


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.

like image 40
Joni Avatar answered Sep 21 '22 08:09

Joni


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.

like image 36
Eric W. Avatar answered Sep 22 '22 08:09

Eric W.