Friday 9 December 2011


About the Java Technology

Java technology is both a programming language and a platform.
The Java Programming Language

The Java programming language is a high-level language that can be characterized by all of the following buzzwords:

Simple
Object oriented
Distributed
Multithreaded
Dynamic
Architecture neutral
Portable
High performance
Robust
Secure

Each of the preceding buzzwords is explained in The Java Language Environment , a white paper written by James Gosling and Henry McGilton.

In the Java programming language, all source code is first written in plain text files ending with the .java extension. Those source files are then compiled into .class files by the javac compiler. A .class file does not contain code that is native to your processor; it instead contains bytecodes — the machine language of the Java Virtual Machine1 (Java VM). The java launcher tool then runs your application with an instance of the Java Virtual Machine.










Figure showing MyProgram.java, compiler, MyProgram.class, Java VM, and My Program running on a computer.

An overview of the software development process.

Because the Java VM is available on many different operating systems, the same .class files are capable of running on Microsoft Windows, the Solaris™ Operating System (Solaris OS), Linux, or Mac OS. Some virtual machines, such as the Java HotSpot virtual machine, perform additional steps at runtime to give your application a performance boost. This include various tasks such as finding performance bottlenecks and recompiling (to native code) frequently used sections of code.







Figure showing source code, compiler, and Java VM's for Win32, Solaris OS/Linux, and Mac OS

Through the Java VM, the same application is capable of running on multiple platforms.

The Java Platform

A platform is the hardware or software environment in which a program runs. We've already mentioned some of the most popular platforms like Microsoft Windows, Linux, Solaris OS, and Mac OS. Most platforms can be described as a combination of the operating system and underlying hardware. The Java platform differs from most other platforms in that it's a software-only platform that runs on top of other hardware-based platforms.

The Java platform has two components:

The Java Virtual Machine
The Java Application Programming Interface (API)











You've already been introduced to the Java Virtual Machine; it's the base for the Java platform and is ported onto various hardware-based platforms.

The API is a large collection of ready-made software components that provide many useful capabilities. It is grouped into libraries of related classes and interfaces; these libraries are known as packages. The next section, What Can Java Technology Do? highlights some of the functionality provided by the API.
Figure showing MyProgram.java, API, Java Virtual Machine, and Hardware-Based Platform

The API and Java Virtual Machine insulate the program from the underlying hardware.

As a platform-independent environment, the Java platform can be a bit slower than native code. However, advances in compiler and virtual machine technologies are bringing performance close to that of native code without threatening portability.

The terms"Java Virtual Machine" and "JVM" mean a Virtual Machine for the Java platform.



Basics Of Java Architecture

A.1 How Java works

Computer programs are usually written in human-readable source code. The compilers convert this source code into machine-readable (executable) programs. Conventional compilers for programming languages like C++ produce machine-readable code that is executed on a specific hardware; for example, the C++ compiler for windows on x86 hardware compiles source code into executable programs, which can run only on Intel x86 compatible hardware. A C++ program needs to be recompiled for a different platform using the platform-specific compiler.



A.1.1 How Java program compiles?

Java takes a different approach than the platform-specific compilers. The Java compiler generates hardware-independent bytecodes from the source code. These bytecodes can be executed only by a Java Virtual machine (JVM). The compilation process is illustrated on the left side of figure A.1. Java source code is compiled into Java bytecode files, also called as class files.










java compilation and Execution
Figure A.1 Java Source compilation and Execution



The class files are special files with a specific format. They are not machine-readable (executable) and cannot be directly executed on a hardware platform. JVM understands the bytecodes in a class file and can execute it. The Java compiler’s job is to convert the Java source code in a class file, which can be understood and executed by the JVM.



A.1.2 How Java program runs?

The Java Virtual Machine actually executes instructions from the class files. JVM is an abstract computing machine. Like a real computing machine, it has an instruction set. It can run code in Java class (bytecode) format.

At runtime, the class loader fetches the .class file from storage media or network. Each class file is fed to a bytecode verifier to ensure that the class file is correctly formatted and is safe for execution. The byte-verification process slows the process to load the class in JVM, but it is performed only once and not every time the program runs. Figure A.1 explains how the JVM fetches classes from disk or network and then verifies that the bytecodes are safe for executing.

Once the class is loaded (the bytecodes become available to JVM), the bytecodes in .class file are actually fed to the JVM. This bytecode is executed by one of the execution units (interpreter or JIT) of JVM at runtime. The execution units carry out the instructions specified in the bytecodes. The interpreter is the simplest execution unit of the JVM. It reads the bytecode, interprets it and performs the associated function. Interpreters are generally slower that the native-code compilers (like C++ compiler), as they need to lookup for meaning of each byte code during the execution. To speed up the execution, the JVM has another execution unit. It is called the JIT or “Just In Time” compiler. It optimizes the runtime bytecode from a class file by compiling it to native code just before the execution.












A.1.3 JVM and platform independence

Since a Java .class file runs only in the JVM, how will you run the same Java code across multiple platforms? For each platform, a platform-specific JVM is provided. JVM for Windows, Linux and Solaris platforms are available on Sun’s website. You can think of a JVM as a software processor which can be plugged-in any hardware platform to make it execute the .class files. Therefore, the same bytecode runs on multiple platforms without any portability issues as it actually runs on the JVM. The JVM takes care of platform dependent issues at runtime.













The Java Virtual Machine knows nothing about the Java programming language. It only needs to know about the class file format. A class file contains Java Virtual Machine instructions (or bytecodes). Therefore, any language capable of generating a valid class file can be hosted by the Java Virtual Machine. To implement the JVM, you only need to be able to read the Java class file’s format and correctly perform the operations specified in it. For security, the Java Virtual Machine imposes strict format and structural constraints on the byte code in a class file.

No comments:

Post a Comment