Java

Java is a cross-platform, object-oriented, multi-threaded programming language, originally developed by Sun Microsystems, that first appeared in 1995. It is now available as free software under the GNU General Public License (GPL). Java derives some of its syntax from C and C++. Java applications are typically compiled to bytecode that will run on any Java Virtual Machine (JVM), regardless of the hardware platform. Java has gained much popularity as a programming language, and is probably best known for its use in producing small, browser-compatible and secure client-side programs called applets. Most web browsers can run Java applets embedded within web pages. The Java programming language has never been specified in an official standard, although it remains a de facto standard defined through the Java Community Process (JCP).

Today, Java comes in several editions, each designed to be used in different application environments. Java Card, the smallest Java package currently available, enables developers to create small Java-based applications (applets) that can be run securely on smart cards and other devices with limited memory available. It is widely used in SIM cards and ATM cards. The Micro Edition (Java ME) is aimed at software developers engaged in creating embedded applications for use in consumer electronics, from washing machines to mobile phones and PDAs, and in other embedded systems such as electronic engine control systems in vehicles. The Enterprise Edition (Java EE) is designed to facilitate the development of software systems for the large distributed business environment, and for Internet environments. The edition that will probably be of most interest to readers of these pages is the Standard Edition (Java SE), which is chiefly aimed at software developers involved in the production of secure, portable applications for a wide range of desktop environments, including Apple Macintosh, Linux, Microsoft Windows, and Sun Solaris.


The Java Virtual Machine

A Java Virtual Machine (JVM) is a set of software programs and data structures which use a virtual machine model for the execution of programs compiled into bytecode in the form of Java .class files. The programs are usually (though not always) generated using Java source code. A JVM is available for a variety of hardware platforms and operating systems. Because the bytecode is essentially interpreted by the JVM, the same bytecode can be used on any platform for which a JVM is available. The JVM is distributed together with a set of standard class libraries that implement the Java Application Programming Interface (API). The Java API itself is specific to a particular hardware platform and operating system, and must therefore be compatible with the JVM. The JVM and the API together comprise the Java Runtime Environment (JRE) for a particular platform. The JRE on the user's computer allows them to run stand-alone Java applications, or to run Java applets in a Web browser.


Java programs

All Java code is written inside a class, and everything is an object (with the exception of intrinsic data types such as ordinal and real numbers, boolean values, and characters). The traditional "Hello World!" program could be written in Java as shown below.

/* Outputs "Hello, World!" and then exits */

public class HelloWorld {
  public static void main(String[ ] args) {
    System.out.println("Hello, World!");
  }
}

By convention, source files are named after the public class they contain, and have the file extension .java (e.g. "HelloWorld.java"). Before it can be executed by the Java virtual machine, the source code must be compiled into bytecode using a Java compiler, which produces a file with the extension .class (e.g. "HelloWorld.class"). A java source file may contain any number of classes, but only one public class. The compiler generates a class file for each class defined in the source file. The name of each class file is the name of the class it contains (with the file extension .class). An application may consist of any number of classes in different files. For ease of distribution, the classes that make up a large program are often packaged together in a Java archive file with the extension .jar.

The Java Virtual Machine is invoked as an instance of the Java Runtime Environment when the user requests the execution of a Java program . It executes a .class or .jar file either by interpreting it as it stands, or by compiling it into native code immediately prior to (or in some cases during) program execution. Interpreted programs usually run more slowly that programs that are compiled into native executable code. In cases where the JVM compiles the code into native format dynamically using a Just In Time (JIT) compiler, the executable code is cached the first time it runs. In this way, while there is some additional overhead involved in the compilation process the first time the program is used, subsequent program execution is much faster. The code may also be compiled directly into native executable code in a more traditional way using static compilation. This ensures improved performance, although the compiled code cannot subsequently be ported to other platforms.


The Java Software Development Kit

The Java Software Development Kit (SDK) is (somewhat confusingly) made up of the Java Development Kit (JDK) bundled together with additional software, such as application servers and debuggers, and documentation. The JDK is the portion of the SDK that is directly responsible for the creation and execution of Java programs, and includes the Java Runtime Environment (JRE) and lots of example programs. Some of the programming tools included in the JDK are listed below.


The Java Community Process

The Java Community Process (JCP) is a formalised process that allows interested parties to participate in the ongoing development of the Java programming language. The formal documents that are used to describe proposed specifications and technologies are called Java Specification Requests (JSRs). These documents undergo a formal public review before being finalised, following a vote of the JCP Executive Committee. The final JSR provides a reference implementation consisting of a freely available implementation of the technology in source code form, and a Technology Compatibility Kit to verify the API specification. The Java Community Process is itself the subject of a Java Specification Request. JSR 215, finalised in 2006, describes the current version (2.6) of the JCP.


Java applets

Java applets are programs that are embedded within other applications, typically in a Web page displayed in a Web browser. The applet's code is downloaded from an HTTP server when the Web page loads, and runs inside a virtual machine that is incorporated into the user's browser. Applets are normally "sandboxed", which means that they cannot access other files or processes on the client computer, affording the client computer protection from malicious code. Applets that are digitally signed by a trusted source (and therefore may be considered reasonably safe) may be permitted to prompt the user for access to the local file system and network. The following code creates a simple "Hello World!" applet:

// A "Hello World" example
import java.applet.*;
import java.awt.*;
public class HelloWorld extends Applet
{
  public void init() {}
  public void stop() {}
  public void paint(Graphics g)
  {
    g.drawString("Hello World!",20,40);
  }
}

An applet is embedded in a HTML document using the <applet> HTML element. In the HTML document shown below, the applet tag has three attributes:

code="HelloWorld"
width="200"
height="200"

The code attribute specifies the name of the JApplet class, and the width and height attributes specify the width and height of the applet in pixels. Note that applets can also be embedded in HTML documents using either the <object> or <embed> tags, although support for these elements varies from one browser to another. Bear in mind, however, that the <applet> tag has been deprecated by W3C, and the <object> tag is the preferred option, if it is supported.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<!-- Hello.html -->
<html>
  <head>
    <title>Hello World Applet</title>
  </head>
  <body>
    <applet code="HelloWorld" width="200" height="200">
    </applet>
  </body>
</html>

Click here to see an example of a Java applet


Java servlets

Java servlets are small server-side Java programs that generate responses (typically HTML pages) to client requests (typically HTTP requests). An example is shown below.

// Hello.java
import java.io.*;
import javax.servlet.*;

public class Hello extends GenericServlet {
  public void service(ServletRequest request, ServletResponse response)
    throws ServletException, IOException {
      response.setContentType("text/html");
      final PrintWriter pw = response.getWriter();
      pw.println("Hello, world!");
      pw.close();
  }
}


JavaServer Pages

JavaServer Pages (JSPs) are HTML pages that contain embedded Java code, and generate responses (typically HTML pages) to client HTTP requests. The Java code is embedded within the HTML document using special delimiters, <% and %>. The JSP is compiled to a Java servlet the first time it is accessed, after which the servlet generates the response.