Java Is a Platform

Java is a platform for executing programs. In contrast to platforms that consist of physical processors (such as an Intel processor) and operating systems (such as Linux), the Java platform consists of a virtual machine and associated execution environment.

The virtual machine is a software-based processor that presents its own instruction set. The associated execution environment consists of libraries for running programs and interacting with the underlying operating system.

The execution environment includes a huge library of prebuilt classfiles that perform common tasks, such as math operations (trigonometry, for example) and network communications. This library is commonly referred to as the standard class library.

A special Java program known as the Java compiler translates source code into instructions (and associated data) that are executed by the virtual machine. These instructions are commonly referred to as bytecode.

The compiler stores a program's bytecode and data in files having the .class extension. These files are known as classfiles because they typically store the compiled equivalent of classes, a language feature discussed in Chapter 2.

A Java program executes via a tool (such as java) that loads and starts the virtual machine, and passes the program's main classfile to the machine. The virtual machine uses a classloader (a virtual machine or execution environment component) to load the classfile.

After the classfile has been loaded, the virtual machine's bytecode verifier component makes sure that the classfile's bytecode is valid and does not compromise security. The verifier terminates the virtual machine when it finds a problem with the bytecode.

Assuming that all is well with the classfile's bytecode, the virtual machine's interpreter interprets the bytecode one instruction at a time. Interpretation consists of identifying bytecode instructions and executing equivalent native instructions.

NOTE: Native instructions (also known as native code) are the instructions understood by the underlying platform's physical processor.

When the interpreter learns that a sequence of bytecode instructions is executed repeatedly, it informs the virtual machine's Just In Time (JIT) compiler to compile these instructions into native code.

JIT compilation is performed only once for a given sequence of bytecode instructions. Because the native instructions execute instead of the associated bytecode instruction sequence, the program executes much faster.

During execution, the interpreter might encounter a request to execute another classfile's bytecode. When that happens, it asks the classloader to load the classfile and the bytecode verifier to verify the bytecode prior to executing that bytecode.

The platform side of Java promotes portability by providing an abstraction over the underlying platform. As a result, the same bytecode runs unchanged on Windows-based, Linux-based, Mac OS X-based, and other platforms.

NOTE: Java was introduced with the "write once, run anywhere" slogan. Although Java goes to great lengths to enforce portability, it does not always succeed. Despite being mostly platform independent, certain parts of Java (such as the scheduling of threads, discussed in Chapter 7) vary from underlying platform to underlying platform.

The platform side of Java also promotes security by providing a secure environment in which code executes. The goal is to prevent malicious code from corrupting the underlying platform (and possibly stealing sensitive information).

NOTE: Because many developers are not satisfied with the Java language, but believe that the Java platform is important, they have devised additional languages (such as Groovy) that run on the Java platform. Furthermore, Java version 7 includes an enhanced virtual machine that simplifies adapting even more dynamic programming languages (languages that require less-rigid coding; you do not have to define a variable's type before using the variable, for example) to this platform.

Was this article helpful?

0 0

Post a comment