Oleg Oleg - 3 months ago 15
Java Question

Is java bytecode translated directly to assembler code?

I know that

javac
compiles the
*.java
files into
*.class
files , the content of these files is called bytecode.

I assumed each bytecode was translated to an assembler equivalent like ADD or SUB or whatever. If this is true then the final result of the JIT and my intepreter should be the same assembler instructions and at this point I don't see why I would need my
/usr/bin/java
interpreter.

Please correct if any of my statement is wrong.

Answer

Java bytecode is a fairly high level language and there's far from a 1-to-1 relation to assembly instructions. Here are a few things that come to mind:

  • Garbage collection - This is a guarantee of the JVM and typically not considered when talking about programs written natively.
  • Classes - Bytecode still has classes and class hierarchies (including polymorphism, virtual method calls, etc)
  • Type safety - Although there exist type safe assembly languages, the bytecode type system is far more complicated than any ordinary assembly language.
  • Array index checks - Each array access needs to (directly or indirectly) be governed by out of bounds checks. A high level feature that must be dealt with by the VM.
  • Null pointer checks - Many bytecode instructions can implicitly throw nice exceptions such as NullPointerException. The "native counterpart" is to fail silently or segfault.
  • Exception handling - Exception handling in general have performance implications. Sure, native languages such as C++ do support exceptions, but many C++ projects avoid using them for performance considerations.