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.
The first part of understanding the technical details of Java is learning how Java interacts with the Webs 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.
As a language for delivering information on the Web, Java connects to the Webs hypertext markup language (HTML) using a special tag called APPLET. Figure 2.1 summarizes this connection:
Javas 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 pages 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 browsers design and the users 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 documents title. Other tags dont require a corresponding ending tag. For example, you can identify a paragraph start using the <P> tag.
Some tags have attributes, which qualify the tags meaning. For example, the APPLET tag has the attributes Code as well as Height and Width.
Here is a simple HTML document:
<HTML> <HEAD> <TITLE>Example HTML Document</TITLE> </HEAD> <BODY> <P> This is the body of the document. <OL> <LI>This is the first item in an ordered list. <LI>This is the second item. </OL> </BODY> </HTML>
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 http://www.december.com/works/wdg/quickref.html 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 Suns 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.
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:
<HTML> <HEAD> <TITLE>HelloWorld</TITLE> </HEAD> <BODY> <P>This is it! <APPLET Code=HelloWorld.class Width=600" Height=300"> </APPLET> </BODY> </HTML>
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.
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 HelloWorld JAVA SOURCE CODE |
---|
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 http://www.december.com/works/java.html. |
You can place Java code in a file named HelloWorld.java. Next, you have to compile the Java source code using the Java compiler, javac. At the operating system prompt ($), enter:
$ javac HelloWorld.java
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 Developers 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 2.2.Java source code and compilation relationships.
FIGURE 2.3.Java browser display of the HelloWorld applet.
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 Javas 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 computers 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 users computer. Instead, the HotJava browser is customized to the users architecture. The HotJava browser interprets the bytecodes for the particular architecture of the user. This is a key characteristic of Javas technical design.
Javas 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 dont 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, lets 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):
Figure 2.4 summarizes the support rings for Java as it is used for worldwide distribution of information.
FIGURE 2.4.The support ring of systems around Java.
Again, you dont have to know how to set up the entire range of networks, software, and equipment in Javas 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 users experience of the Webs 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.
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 (http://java.sun.com/), 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:
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 objects 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, Javas 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 Internets 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 doesnt 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 Cs 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 Javas security in more detail.
The Java compiler creates bytecodes that are sent to the requesting browser and interpreted on the browsers 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, twos 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 isnt 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. Hoares 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 2.5. Object-orientation in software.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.
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 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.
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 allthe ability to learn.
FIGURE 2.7.The Java-age browser can learn.
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.
Java operation within a Web page.
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 (http://java.sun.com/) for complete details on obtaining the Java Developers 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 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:
The Java Development Kit available from Sun Microsystems includes the following pieces:
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
A document available from the Sun Microsystems Java site (http://java.sun.com/) 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.
Because a HotJava browser downloads code across the network and then executes it on the users 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: