Demystifying Java Internals

Introduction

 
Java, is a technology that makes it easy to develop distributed applications that are programs, executed by multiple computers across a network.   Regardless if it is a local, or wide area network.  Java has expanded the internet role from an arena for communications, to a network on which full-fledged applications execute.  Ultimately, this open-source technology gives an impression of network programming, across diverse platforms.
  
This article illustrates the following underlying contents in detail:
  • Genesis of Java 
  • Java and the World Wide Web
  • The beauty of Java: The “Byte code”
  • Java Framework Configuration
  • Java Features
  • Summary
Before Java, the internet was used primarily for sharing information.  Though, developers soon realized that the World Wide Web could fulfill the needs of business potentials, in 1990.  The WWW is a technology that treats internet resources as linked stuff and it has revolutionized the way people access information.  The web enables internet users to access internet services, without learning sophisticated cryptic commands.  Using webs, corporations can easily provide product information and sell merchandise using the computer.  Java technology takes this a step further by making it possible to serve fully interactive applications, via the web.
 
In particular, Java programs can be embedded into web documents.  Turning a static page into an application that runs on the user's computer.  Java has the potential to change the function of the internet, just as the web has changed the way people access the internet.  In other words, not only will the network provide information, it will also serve as an operating system.
 

Genesis of Java

 
In 1990, Java was conceived by James Gosling, Patrick Naughton, and Ed Frank at Sun Microsystem.  This language was initially known as Oak.  Oak preserved the familiar syntax of C++, but omitted the potentially dangerous features like pointer arithmetic, operator overloading, and explicit resource references.  Oak incorporated memory management directly into the language, freeing the programmer to concentrate on the task to be done, by the program.  As Oak matured, the WWW was growing dramatically and the component team at Sun realized Oak was perfectly suited for internet programming.  Thus, in 1994, they completed work on the product known as WebRunner, an early browser written in Oak.  WebRunner was later renamed HotJava and it showed the power of Oak as an internet development tool.  Finally, in 1995, Oak was renamed Java and introduced on Sun.  Since then, Java's rise in popularity has been dramatic.
  
Java is related to C++, that is a direct descendant of C. Much of the character of Java is inherited from C++.   From C++, Java derives its semantics.  Java is truly an Object-Oriented Programming, case sensitive language.  Many of Java OOP features were influenced by C++.  The original impetus for Java was not the internet.  Instead, the primary motivation was the need for a platform-independent language, that could be used to create software to be embedded in various consumer electronic devices.  The trouble with C and C++ is that they are designed to be compiled for a specific target.  So, an easier and more cost-efficient solution was Java technology, a truly open source technology.
 

Java and the World Wide Web

 
Today, the web acts as a convenient transport mechanism for Java programs.  The Web's ubiquity, has popularized Java as an internet development tool.  Java expands the universe of objects, that can move about freely in cyberspace.  In a network, two very broad categories of objects are transmitted between the server and your personal computer.  They are passive information and dynamic active programs.  For example, when you read your e-mails, you are viewing passive data.  However, the second type of object can be transmitted to your compute, a dynamic, self-executing program.  For example, a program might be provided by the server to display the data properly, that the server is sending.  Dynamic network programs present serious problems in the area of security and portability.  As you will see, Java addresses those concerns effectively, by introducing a new form of program, the applet.
 
Java primarily stipulates two types of programs, applets and applications.  An applet is an application designed to be transmitted over the internet and executed by a Java-compatible web browser.  An applet is a tiny Java program that is dynamically downloaded across the network.  An applet is a type of program that can react to user input and dynamically change.  On the other hand, an application runs on your computer, under the operating system of that computer such as, one application created in the C or C++ language.
  
Java technology provides portable code execution across diverse platforms.  Many types of computers and operating systems are in use throughout the world and many are connected to the internet.  For programs to be dynamically downloaded to the various types of platforms connected to the internet, some means of generating portable executing code is needed.
 

The beauty of Java: The “Byte Code”

 
The Java compiler does not produce executable code to resolve security and portability issues.  Rather, it is bytecode.  The bytecode is a highly optimized set of instructions designed to be executed by the Java run-time system.  It is referred to as the Java Virtual Machine (JVM).  The JVM essentially is an interpreter for bytecode.  Translating a Java program into bytecode helps make it easier to run a program in a wide variety of platforms such as Linux, Windows, and Mac.  The only requirement is the JVM needs to be implemented for each platform.  Once the runtime package exists for a given system, any Java program can execute on it.  Although, the details of the JVM will differ from platform to platform, all interpret the same Java bytecode.  Thus, the interpretation of bytecode is a feasible solution to create a truly portable program.
 
java compiler
 
In fact, most modern technologies such as C++ and C# are designed to be compiled, not interpreted.  Mostly because of performance concerns.  When a program is interpreted, it generally runs substantially slower than it would, if compiled to executable code.  The use of bytecode enables the Java run-time system to execute programs, much faster than you might expect.
 
To execute Java bytecode, the JVM uses a class loader to fetch the bytecodes from a network or disk.  Each class file is fed to a bytecode verifier that ensures the class is formatted correctly and will not corrupt memory when it is executed.  The JIT compiler converts the bytecodes to native code instructions on the user's machine, immediately before execution.  The JIT compiler runs on the user machine and is transparent to the users.  The resulting native code instructions do not need to be ported, because they are already at their destination.  The following figure illustrates how the JIT compiler works.
 
macintosh power pc
 

Prerequisite

 
To write and execute a program written in the Java language, we are supposed to configure our workstation with the following software:
  • Java Development Kit (JDK)
  • Java Virtual Machine (JVM)
  • Eclipse Juno IDE
  • Tomcat Web Server (Required for Servlet and JSP)
  • Notepad++ (optional)


Java Framework Configurations

 
A Java program can be built and compiled either by third-party tools such as Eclipse Juno, or Java development environment tools that require some configuration in the user machine to run programs, whereas third-party tools don't.  As per the open-source nature of Java, such development tools are freely available from the http://java.sun.com website.  The following segments specify each specific tool configuration in details.
 
 

Java Development Kit

 
The JDK was the originally Java development environment, which can create and display a graphical application.  The JDK consists of a library of standard classes (core Java API) and collections of utilities for building, testing, and documenting a Java program.  You need the core Java API to access the core functionality of the Java language.  The core Java API includes underlying language constructs, as well as graphic, network, garbage collection, and file input and output capabilities.  Here, are the JDK utilities: 
 
jdk utilities
After installing and configuring the JDK, the tools to build and run a Java application are illustrated in the following figure:
 
java source code
 

First Hello World Java Program

 
The Java source code can be written with a simple text editor such as Notepad.  The best IDE is Eclipse Juno. which provides numerous development templates. As you can see, the following console base Java program simply prints a “Hello world” text, over the screen.
 
The code defines a Java class called HelloWorld that contains a single method called main().  When the Java interpreter tries to execute the HelloWorld class, it will look for a method called main().  The VM will execute this function to run the program.
  1. public class HelloWorld {  
  2.  public static void main(String[] args) {  
  3.   System.out.println("Hello Word!");  
  4.  }  
  5. }   
Thereafter, save the file with the “Helloworld.java” name somewhere on disk.  Remember, the class name must be a file name with the *.java extension.  To compile the sample program, execute the compiler, javac, specifying the name of the source file on the command prompt.  The javac compiler creates a file called HelloWord.class that contains the bytecode version of the program.  To actually run the program, you must use the Java Interpreter, called Java.  To do so, pass the class name as a command-line argument.  The following figure depicts the entire lifecycle of the Java compilation process:
 
java compiler and java interpretation
 
So, what we can surmise from the previously mentioned sample is that a Java program is first compiled and later interpreted.  The following illustrates the various configuration tools, employed during the compilation process.
 
By adding a few comments to your Java source code, you make it possible for Javadoc to automatically generate HTML documentation for your code.  After adding a few comments to your source code, issue the Javadoc command and it will make s a couple of files, as in the following:
 
javadoc command
 
It is possible to examine the bytecodes of a compiled class file and identify its accessible variable and functions.  The java utility creates a report that shows not only what functions and variables are available, but what the code actually does at the very low level as in the following:
 
javap utility
 

Java Features

 
In this section, we will look briefly at the major characteristics that make Java such a powerful development tool.  This includes cross-platform execution code support, multithreading, security, and object-oriented features, as in the following.
 

Architecture-Neutral

 
One of the main problems facing programmers, is that no guarantee exists that if you write a program today, it will run tomorrow.  Even, on the same machine.  Processor and operating system upgrades and change in core system resources can make a program malfunction.  Java solves all of these issues by introducing “write once, run anywhere” anytime forever.
 

Object-oriented

 
Java is an object-oriented language.  That is, it has OOP features incorporated into the language.  We can write reusable, extensible and maintainable software by OOP support.  Java supports all object-oriented mechanisms such as Abstraction, Inheritance, Polymorphism, and Encapsulation.
 

Interpreted and High Performance

 
Java enables the creation of cross-platform programs by compiling into an intermediate representation called bytecode.  This code can be interpreted on any system that provides a JVM.  Java is specially designed to perform well on very low-power CPUs.  Java bytecodes easily translate directly into native machine code for very high-performance code,  using the JIT compiler.
 

Multithreading support

 
Java was designed to meet the real-world requirements of creating interactive network programs.  To accomplish this, Java supports multithreaded programming, that allows you to write programs that do many things simultaneously.  The Java run-time system comes with an elegant, yet sophisticated solution for multi-process synchronization that enables you to construct smoothly running interactive systems.
 

Distributed application

 
Java is designed for the distributed environment of the internet because it handles TCP/IP protocols.  This allows objects on two computers to execute procedures remotely. Java has recently revived these interfaces in a package called RMI.  This feature brings unparalleled abstraction to client/server programming.
 

Strictly typed language

 
Java programs provide a robust programming impression, by which you restrict a few key areas to force finding your mistakes, early in the program, at design time.  Java frees you from having to worry about many of the most common causes of programming errors, because Java is a strictly typed language.  It checks your code at compile time. However, it also checks it at run time.
 

Security

 
Security is probably the main problem, facing internet developers.  Users are typically afraid of two things, confidential information being compromised and their computer systems being corrupted or destroyed by hackers.  Java built-in security addresses both of these concerns.  The Java security model has three primary components, the class loader, bytecode verifier, and SecurityManager.  We will dig deeper into these concepts in later articles.
 

Final Note

 
This article introduced you to the history of Java, its effect on the WWW, and the underlying Java architecture.  This chapter explains the role of a Java development kit in writing program code.  We saw how to write simple Java console applications, using JDK utilities such as java, javac, Javadoc and so on.  Finally, we come to an understanding of why Java is so popular, in the developer community, by specifying its advantage over other languages.  After reading this article, one can easily start programming in Java.