Translate

Friday 8 June 2012

Java's Magic: The Bytecode

The key that allows Java to solve both the security and the portability problems is that the output of a Java compiler is not executable code. Rather, it is bytecode. Bytecode is a set of instructions designed to be executed by the Java run-time system known as Java Virtual Machine (JVM). That is, in its standard form, the JVM is an interpreter for bytecode. 
Translating a Java program into bytecode helps makes it easier to run a program in a wide variety of environments. The reason is straightforward: only the JVM needs to be implemented for each platform. Once the run-time package exists for a given system, any Java program can run on it. Although the details of JVM will differ from platform to platform, all interpret the same Java bytecode. 
The fact that a Java program is interpreted also helps to make it secure. Because the execution of every Java program is under the control of the JVM, the JVM can contain the program and prevent it from generating side effects outside of the system. 
Although Java was designed for interpretation, there is technically nothing about Java that prevents on-the-fly compilation of bytecode into native code. Along these lines, Sun supplies its Just In Time (JIT) compiler for bytecode, which is included in the Java 2 release. When JIT compiler is part of the JVM, it compiles bytecode into the executable code in real time, on a piece-by-piece, demand basis. It is not possible to compile an entire Java program into executable code all at once, because Java performs various run time checks that can be done only at run time. Instead, the JIT compiles code as it is needed, during execution.

No comments:

Post a Comment