Skip to main content.

Web Based Programming Tutorials

Homepage | Forum - Join the forum to discuss anything related to programming! | Programming Resources

Java Unleashed

Java Unleashed--ch02.htm Previous Page toc Index Next Page

Chapter 2

Java’s Design Is Flexible and Dynamic

The Java programming language is uniquely suited for distributing executable content over networks. Java also offers a set of functions similar to many other programming languages. This chapter presents an overview of the technical design of Java. I begin with a minimal example of a “hello world” Java program. This should help you understand how Java and HTML connect. Using this information, you can then try out some of the Java programs shown in later parts of this book.

Java also has specialized characteristics. In the second part of this chapter, I discuss in more technical detail how Java supports executable, distributed applications.

A Hello to Java

The first part of understanding the technical details of Java is learning how Java interacts with the Web’s hypertext. The example shown in this section demonstrates how a special tag of the hypertext markup language (HTML) associates a Java program called an applet to a page on the Web. Viewed through a Java-enabled Web browser, a page with a Java applet can come alive with animation or interaction.

Java’s Connection to the Web

As a language for delivering information on the Web, Java connects to the Web’s hypertext markup language (HTML) using a special tag called APPLET. Figure 2.1 summarizes this connection:

  1. In response to a request from a user of a Web browser, a document on a Web server written in HTML is downloaded to the user’s browser.
  2. If the HTML document contains an APPLET tag and the user’s Web browser is Java-enabled, the browser looks for the value of the Code attribute which identifies the Java bytecodes defining the applet.
  3. The applet bytecodes are downloaded from the Web server (or possibly some other Web server or network site identified by attributes of the APPLET tag) and placed on the user’s host computer.
  4. The user’s Java-enabled browser interprets these bytecodes and runs the applet in the user’s browser. The applet commonly will provide a visual indication that it is operating and possibly accept input from some combination of the user’s cursor position, mouse buttons, or keyboard. Once the applet is downloaded, it need not be downloaded again, even if the applet code defines repeated loops or other interaction. The user might use a downloaded applet several times over the course of an online session without any more network retrievals.
Figure FIGURE 2.1.

Java’s connection to the Web through the APPLET tag.

A technical understanding of Java also requires a familiarity with HTML. HTML is the markup language used to create the documents displayed in Web browsers. HTML is not a layout language for describing how a page of hypertext should look (although there are many features of HTML that can be used to manipulate a page’s appearance). Rather, HTML tags the structure of a document and the meaning of text, so that a browser can display it in a scheme based on that browser’s design and the user’s preferences for the font size, style, and other features.

An HTML document consists of text and tags that mark the structure of the document. Tags in an HTML document are delimited by the brackets < and >. Some tags always appear in a pair, as a start and end tag. For example, you can identify the title of an HTML document by placing the tags <TITLE> and </TITLE> around the text of the document’s title. Other tags don’t require a corresponding ending tag. For example, you can identify a paragraph start using the <P> tag.

Some tags have attributes, which qualify the tag’s meaning. For example, the APPLET tag has the attributes Code as well as Height and Width.

Here is a simple HTML document:



   <TITLE>Example HTML Document</TITLE>




   This is the body of the document.


   <LI>This is the first item in an ordered list.

   <LI>This is the second item.




When a Web browser interprets these HTML tags and text, it displays the document without the brackets < and >. A text-only browser renders this simple HTML example as

Example HTML Document

This is the body of the document.

    1. This is the first item in an ordered list.

    2. This is the second item.

The document contains HTML tags presented in a reference table, showing many more features of HTML that are available. The simple HTML example shown here is recognized by Sun’s HotJava and other Java-enabled browsers and should be enough to get you started in understanding how HTML connects to Java and testing simple applets.

A Simple Java Program

The APPLET tag in an HTML document identifies the name of a Java program called an applet to be included in a Web page. The name of the applet is called its class name. This name is associated with the executable bytecodes that run the applet.

For example, the following HTML example demonstrates how you can include an applet in a Web document. If you want to test this, put the following lines in a file called HelloWorld.html:






  <P>”This is it!”

  <APPLET Code=”HelloWorld.class” Width=”600" Height=”300">




Note that there is an open APPLET tag, <APPLET>, and a close APPLET tag, </APPLET>. The attributes shown here are Code, to identify the class file which contains the Java bytecodes and the Width and Height attributes, measured in pixels, to describe how much room should be reserved on the Web page for the applet.

Java uses an APPLET tag to place executable content in an HTML document.

General Format


   Codebase = “path to directory containing class files”

   Code = “name of class file”

   Width = “width of applet in pixels”

   Height = “height of applet in pixels”>

   <PARAM Name=”parameter name” Value=”value of parameter”>

   <PARAM Name=”parameter name” Value=”value of parameter”>


The parameter values are given to the applet for use in its computations.

Here is a sample use of the APPLET tag:


   Codebase  = “”

   Code = “TumbleItem.class”

   Width = “400”

   Height = “95”>

   <PARAM Name=”maxwidth” Value = “100”>

   <PARAM Name=”nimgs” Value = “16”>

   <PARAM Name=”offset” Value = “-57”>

   <PARAM Name=”img” Value = “Âimages/tumble”>


Of course, you need to create the Java source code for the applet named HelloWorld. You can find more details on programming in Java in Chapter 12, “Java Language Fundamentals.” For now, here is a minimal Java applet as a simple demonstration:

import java.awt.Graphics;


 A first hello.


public class HelloWorld extends java.applet.Applet {

   public void init() {

      resize(600, 300);


   public void paint(Graphics context) {

      context.drawString(“Hello, world!”, 50, 100);


The source code for HelloWorld is on the CD-ROM that accompanies this book. I also provide the source code for the HelloWorld and other introductory Java applets at my book support Web page for Presenting Java at

You can place Java code in a file named Next, you have to compile the Java source code using the Java compiler, javac. At the operating system prompt ($), enter:

$ javac

If there are no errors, the compiler will create a file named HelloWorld.class that contains the bytecodes for the HelloWorld applet.

So at this point, you have the following:

Figure 2.2 summarizes the Java source code and compilation relationships.

If you have a Java-enabled browser, you can test this applet. Use the browser to open the file HelloWorld.html. Alternatively, you can also use the applet viewer supplied with the Java Developer’s Kit (JDK) to view applets without having to make an HTML page to reference them. Figure 2.3 shows what this example looks like in Netscape Navigator.

Figure FIGURE 2.2.

Java source code and compilation relationships.

Figure FIGURE 2.3.

Java browser display of the HelloWorld applet.

Java Technical Overview

The preceding example concretely demonstrates the connection of Java applets to the Web through the APPLET tag. But this is only a view of Java from a very beginning perspective. To help you understand Java’s design and potential, this section provides a technical and conceptual overview of the language and its role in online communication.

Java is an object-oriented programming language that is used in conjunction with Java-enabled Web browsers. These browsers can interpret the bytecodes created by the Java language compiler. The technical design of Java is architecture neutral. The term architecture in this sense refers to computer hardware. For example, your computer’s architecture could be an IBM personal computer with an Intel 386 chip. Programmers can create Java programs without having to worry about this underlying architecture of a user’s computer. Instead, the HotJava browser is customized to the user’s architecture. The HotJava browser interprets the bytecodes for the particular architecture of the user. This is a key characteristic of Java’s technical design.

The Network Communication Support Ring Around Java

Java’s technical characteristics also place it within the larger context of online communication. We can step back from the Java source and bytecode files and look at the “big picture” of how Java fits into cyberspace.

The operation of Java and Java-enabled browsers on the Web requires the interoperation of a variety of network systems. Of course, you don’t have to understand the interoperation of all of these systems to use Java or a Java-enabled browser. But, stepping back a bit from the applet-scale view of Java, we can look at its place in a “support ring” of networks and applications.

The goal of Java is to bring executable content to the Web. When installed, a Java-enabled browser can provide an interface to animated and interactive applications. To view and interact with these applications, you must have a computer with a Java-enabled browser installed. If you want to download content from all over the Web, of course you also must have an Internet connection.

Beginning with the widest context for the operation of the Java technology, let’s take a look at the systems necessary to support Java when delivering information globally (again, Java can be used on local networks not requiring the Internet, collapsing the set of support rings described here considerably):

  1. Cyberspace is the mental model people have for communicating or interacting online or through computers. Cyberspace activity includes variety of information, communication, and interaction. Cyberspace can be thought of as consisting of non-networked and networked regions. The networked region in cyberspace includes activity on connected local, regional, and global computer networks. The non-networked region might be standalone personal computer applications like word processors or CD-ROMs that contain no network references.
  2. The Internet computer network serves as a vehicle for data communication for many information dissemination protocols. Through gateways, many other networks in cyberspace can exchange data with the Internet. Because of this and also because of the large amount of information available on it, the Internet serves as a common ground for the networked region of cyberspace.
  3. The Web is an application that relies on a client/server model for data communication for distributing hypermedia. While the Web can operate on local networks that have no connection to the Internet, the Web is popularly known for its collection of information that is available globally through the Internet.
  4. A Web client, known as a browser, is a software program that interprets and displays information disseminated using a variety of Internet information protocols. A Web browser is a user’s interface into the Web. A pre-Java Age (Mosaic class) browser usually operates in conjunction with a variety of helper applications to display multimedia. A Java-enabled browser can dynamically learn new protocols and media content types, so that it need not rely on these helper applications. However, a Netscape 2.0 browser, while Java-enabled, still makes use of helper applications, because the entire content of the Web isn’t Java-ized.
  5. HTML is used to create hypertext for the Web and marks the semantic structure of Web documents. HTML consists of tags and entities that identify the structure and meaning of text in documents. Documents contain references to other resources using a system of Uniform Resource Locators (URLs).
  6. The HTML APPLET tag associates Java applications with HTML documents. This tag occurs in an HTML document and identifies a Java applet that will be placed in that document.
  7. A Java programmer prepares a file of human-readable Java source code. This source code defines an applet, which is a class in the hierarchy of classes that make up the Java language.
  8. A Java programmer compiles a Java source code and makes the resulting bytecodes available for use through a reference to them in an APPLET tag in an HTML document.
  9. HotJava, or any other Java-enabled browser, downloads hypertext as well as the executable bytecodes of the applet. The browser interprets and displays the applet, allowing a user to view or interact with the applet.

Figure 2.4 summarizes the support rings for Java as it is used for worldwide distribution of information.

Figure FIGURE 2.4.

The support ring of systems around Java.

Again, you don’t have to know how to set up the entire range of networks, software, and equipment in Java’s “support ring.” All you need is to install a Java-enabled browser on your Internet-accessible system. From your point of view as a user, your main focus is your browser, or the interior fourth ring, of Figure 2.4. A Java programmer, in contrast, inhabits the seventh ring, and tries to meld the user’s experience of the Web’s hypertext with the specialized content Java makes possible.

You can use Figure 2.4 to help place yourself in cyberspace as you fulfill different roles as an information user or producer.

Characteristics of Java as a Programming Language

While users may want to have some awareness of how Java fits into online communication, programmers need to understand more specific technical characteristics of Java. The description in this section introduces many terms programmers should learn.

According to the information provided by Sun Microsystems (, Java is a

“ …simple, object-oriented, distributed, interpreted, robust, secure, architecture neutral, portable, high-performance, multithreaded, and dynamic language.”

This characterization identifies the key technical features of Java as shown in the following sections.


The developers of Java based it on the C++ programming language, but removed many of the language features that are rarely used or often used poorly. C++ is a language for object-oriented programming and offers very powerful features. However, as is the case with many languages designed to have power, some features often cause problems. Programmers can create code that contains errors in logic or is incomprehensible to other programmers trying to read it. Because the majority of the cost of software engineering is often code maintenance rather than code creation, this shift to understandable code rather than powerful but poorly understood code can help reduce software costs. Specifically, Java differs from C++ (and C) in these ways:

  1. Java does not support the struct, union, and pointer data types.
  2. Java does not support typedef or #define.
  3. Java differs in its handling of certain operators and does not permit operatoroverloading.
  4. Java does not support multiple inheritance.
  5. Java handles command-line arguments differently than C or C++.
  6. Java has a String class as part of the java.lang package. This differs from the null-terminated array of characters as used in C and C++.
  7. Java has an automatic system for allocating and freeing memory (garbage collection), so it is unnecessary to use memory allocation and de-allocation functions as in C and C++.


Like C++, Java can support an object-oriented approach to writing software. Ideally, object-oriented design can permit the creation of software components that can be reused.

Object-oriented programming is based upon modeling the world in terms of software components called objects. An object consists of data and operations that can be performed on that data called methods. These methods can encapsulate, or protect, an object’s data because programmers can create objects in which the methods are the only way to change the state of the data.

Another quality of object-orientation is inheritance. Objects can use characteristics of other objects without having to reproduce the functionality in those objects that supports those characteristics. Inheritance thus helps in software re-use, because programmers can create methods that do a specific job exactly once.

Another benefit of inheritance is software organization and understandability. By havingobjects organized according to classes, each object in a class inherits characteristics from parent objects. This makes the job of documenting, understanding, and benefiting from previous generations of software easier, because the functionality of the software has incrementally grown as more objects are created. Objects at the end of a long inheritance chain can be very specialized and powerful. Figure 2.5 summarizes the general qualities of data encapsulation, methods, and inheritance of an object-oriented language.

Technically, Java’s object-oriented features are those of C++ with extensions from Objective C for dynamic method resolution.


Unlike the languages C++ and C, Java is specifically designed to work within a networked environment. Java has a large library of classes for communicating using the Internet’s TCP/IP protocol suite, including protocols such as HTTP and FTP. Java code can manipulate resources via URLs as easily as programmers are used to accessing a local file system using C or C++.


When the Java compiler translates a Java class source file to bytecodes, this bytecode class file can be run on any machine that runs a Java interpreter or Java-enabled browser. This allows the Java code to be written independently of the users’ platforms. Interpretation also eliminates the compile and run cycle for the client because the bytecodes are not specific to a given machine but interpreted.


Robust software doesn’t “break” easily because of programming bugs or logic errors in it. A programming language that encourages robust software often places more restrictions on the programmer when he or she is writing the source code. These restrictions include those on data types and the use of pointers. The C programming language is notoriously lax in its checking of compatible data types during compilation and runtime. C++ was designed to be more strongly typed than C; however, C++ retains some of C’s approach toward typing. In Java, typing is more rigorous: a programmer cannot turn an arbitrary integer into a pointer by casting, for example. Also, Java does not support pointer arithmetic but has arrays instead. These simplifications eliminate some of the “tricks” that C programmers could use to access arbitrary areas of memory. In particular, Java does not allow the programmer to overwrite memory and corrupt other data through pointers. In contrast, a C programmer often can accidentally (or deliberately) overwrite or corrupt data.


Because Java works in networked environments, the issue of security is one that should be of concern to developers. Plans are in the works for Java to use public-key encryption techniques to authenticate data. In its present form, Java puts limits on pointers so that developers cannot forge access to memory where not permitted. These aspects of Java enable a more secure software environment. The last section of this chapter outlines the layers of Java’s security in more detail.

Architecture Neutral

The Java compiler creates bytecodes that are sent to the requesting browser and interpreted on the browser’s host machine, which has the Java interpreter or a Java-enabled browser installed.


The quality of being architecture neutral allows for a great deal of portability. However, another aspect of portability is how the hardware interprets arithmetic operations. In C and C++, source code may run slightly differently on different hardware platforms because of how these platforms implement arithmetic operations. In Java, this has been simplified. An integer type in Java, int, is a signed, two’s complement 32-bit integer. A real number, float, is always a 32-bit floating-point number defined by the IEEE 754 standard. These consistencies make it possible to have the assurance that any result on one computer with Java can be replicated on another.


Although Java bytecodes are interpreted, the performance sometimes isn’t as fast as direct compilation and execution on a particular hardware platform. Java compilation includes an option to translate the bytecodes into machine code for a particular hardware platform. This can give the same efficiency as a traditional compile and load process. According to Sun Microsystems testing, performance of this bytecode to machine code translation is “almost indistinguishable” from direct compilation from C or C++ programs.


Java is a language that can be used to create applications in which several things happen at once. Based on a system of routines that allow for multiple “threads” of events based on C. A. R. Hoare’s monitor and condition paradigm, Java presents the programmer with a way to support real-time, interactive behavior in programs.


Unlike C++ code, which often requires complete recompilation if a parent class is changed, Java uses a method of interfaces to relieve this dependency. The result is that Java programs can allow for new methods and instance variables in objects in a library without affecting their dependent client objects.

Figure FIGURE 2.5. Object-orientation in software.

HotJava Is a New Kind of Web Browser

The HotJava browser that showcases Java marks the start of a new generation of smart browsers for the Web. Not constrained to a fixed set of functionality, the HotJava browser can adjust and learn new protocols and formats dynamically. Developers of Web information using Java need no longer be constrained to the text, graphics, and relatively low-quality multimedia of the fixed set available for Web browsers in the pre-Java age. Instead, the HotJava browser opens possibilities for new protocols and new media formats never before seen on the Web.

Through the past half-decade of development of the World Wide Web, new browser technologies have often altered the common view of what the Web and online communication could be. When the Mosaic browser was released in 1993, it rocketed the Web to the attention of the general public because of the graphical, seamless appearance it gave to the Web. Instead of a disparate set of tools to access a variety of information spaces, Mosaic dramatically and visually integrated Internet information. Its point-and-click operation changed ideas about what a Web browser could be, and its immediate successor, Netscape, has likewise grown in popularity and continued to push the bounds of what is presented on the Web.

HotJava, however, marks a new stage of technological evolution of browsers. HotJava breaks the model of Web browsers as only filters for displaying network information; rather, a Java-age browser acts more like an intelligent interpreter of executable content and a displayer for new protocol and media formats. The 2.0 release and above of Netscape Communications’ Navigator browser is Java-enabled. Netscape justifiably characterizes their browser as a platform for development and applications rather than just a Web browser.

Pre-Java Browsers

The earliest browser of the Web was the line-mode browser from CERN. The subsequent Mosaic-class browsers (Mosaic and Netscape from 1993 to mid-1995) dramatically opened the graphical view of the Web. However, the Mosaic-type browsers acted as an information filter to Internet-based information. Encoded into these browsers was knowledge of the fundamental Internet protocols and media formats (such as HTTP, NNTP, Gopher, FTP, HTML, GIF). The browsers matched this knowledge with the protocols and media formats found on the Net, and then displayed the results. Figure 2.6 illustrates this operation as the browser finds material on the Net and interprets it according to its internal programming for protocols or common media formats. These browsers also used helper applications to display specialized media formats such as movies or sound.

Figure FIGURE 2.6. Pre-Java browsers acted as filters.

A pre-Java browser was very knowledgeable about the common protocols and media formats about the network (and therefore very “bulky”). Unfortunately, a pre-Java browser could not handle protocols for which it had not been programmed or media formats for which it did not have a helper application available. These are the technical shortcomings that a Java-age browser addresses.

Java-Age Browsers

A Java-age browser is lightweight because it actually has no pre-defined protocols or media formats programmed into its core functionality; instead the core functionality of a HotJava browser consists of the capability to learn how to interpret any protocol or media format. Of course, the HotJava browser is told about the most common protocols and formats as part of its distribution package. In addition, any new format or protocol that a Java programmer might devise, a HotJava browser can learn.

As Figure 2.7 shows, a Java-age browser is “lightweight,” not coming with a monolithic store of knowledge of the Web, but with the most important capbility of all—the ability to learn.

Figure FIGURE 2.7.

The Java-age browser can learn.

Java in Operation

Another way to put the Java language, a Java-enabled browser, and the larger context of online communications into perspective is to review the processes that occur when a user with a Java-enabled browser requests a page containing a Java applet. Figure 2.8 shows this process.

  1. The user sends a request for an HTML document to the information provider’s server.
  2. The HTML document is returned to the user’s browser. The document contains the APPLET tag, which identifies the applet.
  3. The corresponding applet bytecode is transferred to the user’s host. This bytecode had been previously created by the Java compiler using the Java source code file for that applet.
  4. The Java-enabled browser on the user’s host interprets the bytecodes and provides display.
  5. The user may have further interaction with the applet but with no further downloading from the provider’s Web server. This is because the bytecode contains all the information necessary to interpret the applet.
Figure FIGURE 2.8.

Java operation within a Web page.

Java Software Components

Another aspect of the technical make-up of the Java environment is the software components that comprise its environment. See the Sun Microsystems Java site ( for complete details on obtaining the Java Developer’s Kit (JDK). Programmers need to learn the vocabulary of the pieces of the JDK as well as terms for what can be created with it.

Java Language Constructs

Java is the programming language used to develop executable, distributed applications for delivery to a Java-enabled browser or the Java Interpreter. A Java programmer can create the following:

Java Distribution Software

The Java Development Kit available from Sun Microsystems includes the following pieces:

The Java Application Programming Interface (API)

The Java Application Programming Interface (API) is a set of classes that are distributed with the JDK and which programmers can use in Java applications. The documentation of the API that is provided online is key reference material for Java programmers. The API consists of the packages in the Java language. The API documentation includes a list of

The Java Virtual Machine Specification

A document available from the Sun Microsystems Java site ( called “The Java Virtual Machine,’ specifies how the Java language is designed to exchange executable content across networks. The aim of this specification is to describe Java as a non-proprietary, open language that may be implemented by many companies and sold as a package.

The Java Virtual Machine specification describes in abstract terms how Java operates. This leaves the details of implementation up to the programmers who creates Java interpreters and compilers. The Java Virtual Machine specification also concretely defines the specific interchange format for Java code. This is called “The Java Interchange Specification.”

The other part of the Virtual Machine specification defines the abstractions that can be left to the implementor. These abstractions are not related to the interchange of Java code. These include, for example, management of runtime data areas, garbage collection algorithms, the implementation of the compiler and other Java environment software, and optimization algorithms on compiled Java code.

Java Security

Because a HotJava browser downloads code across the network and then executes it on the user’s host, security is a major concern for Java-enabled browser users and Java programmers.

HotJava includes several layers of security, including the following:


The Java programming language is uniquely designed to deliver executable content across networks. As a language, it flexibly offers features for programmers to create a variety of software. Java also assures interoperability among platforms as well as security:

Previous Page toc Index Next Page