Programming Tutorials

Java magic

By: aathishankaran in Java Tutorials on 2007-02-01  

Java's Magic: The Byte code

The key that allows java to solve both the security and the portability problems just described is that the output of a java compiler is not executable code. Rather, it is byte code, byte code is a highly optimized set of instructions designed to be executed by the java runtime system, which is called the java virtual machine (JVM). That is, in its standard form, the JVM is an interpreter for byte code, this may come as a bit of a surprise. As you know, C+++ is compiled to executable code. In fact, most modern languages are designed to be compiled, not interpreted mostly because of performance concerns. However, the fact that a java program is executed by the JVM helps solve the major problems associated with downloading programs over the Internet. Here is why.

Translating a java program into bytecode helps makes it much easier ot run a program in a wide variety of environments. The reason is straightforward: only the JVM needs to be implemented for each platform, all interpret the same java bytecode. If a java program were compiled to native code, the different versions of the same program would have to exist for each type of CPU connected to the Internet. This is, of course, not a feasible solution. Thus, the interpretation of bytecode is the easiest way to create truly portable programs.

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. As you will see, safety is also enhanced by certain restrictions that exist in the java language.

When a program is interpreted, it generally runs substantially slower than it would run if compiled to executable code. However, with java, the differential between the two is not so great. The use of bytecode enables the java run-time system to execute programs much faster than you might expect.

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, Oracle supplies its Just In Time (JIT) compiler for bytecode, which is included in the java 2 release. When the JIT compiler is part of the JVM, it compiles bytecode into executable code in real time, on a piece-by-piece, demand basis. It is important to understand that 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. However, the just-in-time approach still yields a significant performance boost. Even when dynamic compilation is applied to bytecode, the portability and safety features still apply, because the run-time system (which performs the compilation) still is in charge of the execution environment. Whether your java program is actually interpreted in the traditional way or compiled on-the-fly, its functionality is the same.






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in Java )

Latest Articles (in Java)