Java Applet

Java Overview


CONTENTS

Anyone who's had anything to do with computers in the '90s knows that the Internet is all the rage. The immense growth of this global computer network has not only created a handy way to download files and information but has also sparked major controversies over freedom of speech, copyright law, and computer security. Hardly a day goes by without the Internet making the news.

But not all Internet activity is steeped in controversy. One of the more positive Internet newsmakers has been the release of Java, a computer programming language that enables folks like you and me to easily create applications that can be used across the Internet without worrying about platform compatibility or network security. The two types of Java applications-applets for use within World Wide Web pages and stand-alone Java applications-are guaranteed to do more to liven up the World Wide Web than even the most heated controversy.

Java, however, was conceived long before its suitability for the Internet was noted and taken advantage of. You may be surprised to learn that Java was developed for a very different use. In fact, "Java" isn't even the language's original name. In this chapter, you'll get a quick look at Java's history, as well as learn why Java is an excellent tool for creating Internet applications.

The Java Story

Back in 1990, a gentleman by the name of James Gosling was given the task of creating programs to control consumer electronics. Gosling and his team of people at Sun Microsystems started designing their software using C++, the language that most programmers were praising as the next big thing because of its object-oriented nature. Gosling, however, quickly found that C++ was not suitable for the projects he and his team had in mind. They ran into trouble with complicated aspects of C++ such as multiple inheritance of classes and with program bugs such as memory leaks. Gosling soon decided that he was going to have to come up with his own, simplified computer language that would avoid all the problems he had with C++.

Although Gosling didn't care for the complexity of languages such as C++, he did like the basic syntax and object-oriented features of the language. So when he sat down to design his new language, he used C++ as its model, stripping away all the features of C++ that made that language difficult to use with his consumer-electronics projects. When Gosling completed his language-design project, he had a new programming language that he named Oak. (The story goes that the name Oak came to Gosling as he gazed out his office window at an oak tree.)

Oak was first used in something called the Green project, wherein the development team attempted to design a control system for use in the home. This control system would enable the user to manipulate a list of devices, including TVs, VCRs, lights, and telephones, all from a hand-held computer called *7 (Star Seven). The *7 system featured a touch-sensitive screen that the owner used to select and control the devices supported by the control.

NOTE
The *7 screen display featured a number of animated figures, of which Duke (now the little guy considered to be the Java mascot) was one. Once you get involved with Java, you're liable to see a lot of Duke, who pops up on the Sun Microsystems Web site (Figure 1.1), and who is featured in some of Sun's sample Java applets.

Figure 1.1 : Duke has become the Java mascot.

The next step for Oak was the video-on-demand (VOD) project, in which the language was used as the basis for software that controlled an interactive television system. Although neither *7 nor the VOD project led to actual products, they gave Oak a chance to develop and mature. By the time Sun discovered that the name "Oak" was already claimed and they changed the name to Java, they had a powerful, yet simple, language on their hands.

More importantly, Java was a platform-neutral language, which meant that programs developed with Java could run on any computer system with no changes. This platform independence was attained by using a special format for compiled Java programs. This file format, called byte-code, could be read and executed by any computer system that has a Java interpreter. The Java interpreter, of course, must be written specially for the system on which it will run.

In 1993, after the World Wide Web had transformed the text-based Internet into a graphics-rich environment, the Java team realized that the language they had developed would be perfect for Web programming. The team came up with the concept of Web applets, small programs that could be included in Web pages, and even went so far as to create a complete Web browser (now called HotJava) that demonstrated the language's power.

In the second quarter of 1995, Sun Microsystems officially announced Java. The "new" language was quickly embraced as a powerful tool for developing Internet applications. Netscape Communications, the developer of the popular Netscape Navigator Web browser (Figure 1.2), added support for Java to its new Netscape Navigator 2.0. Other Internet software developers are sure to follow suit, including Microsoft, whose Internet Explorer 3 (currently in beta) offers Java support. After more than five years of development, Java has found its home.

Figure 1.2 : The new Netscape Navigator 2.0 Web browser is Java capable.

Introducing Java

By now, you may be curious why Java is considered such a powerful tool for Internet development projects. You already know that Java is a simplified version of C++. Anyone who has struggled with learning C++ knows that the key word in the previous sentence is "simplified." C++ added so much to the C language that even professional programmers often have difficulty making the transition.

According to Sun Microsystems, Java is "simple, object-oriented, statically typed, compiled, architecture neutral, multi-threaded, garbage collected, robust, secure, and extensible." That's a mouthful, but this description of Java probably doesn't help you understand the language much. The following list of Java's attributes, however, should clear out some of the cobwebs:

As you can tell from the preceding list of features, a great deal of thought went into creating a language that would be fairly easy to use but still provide the most powerful features of a modern language like C++. Thanks to features such as automatic garbage collection, programmers can spend more time developing their programs rather than wasting valuable man-hours hunting for hard-to-find memory-allocation bugs. However, features such as Java's object-oriented nature, as well as its ability to handle multiple threads of execution, ensure that the language is both up-to-date and powerful.

Java Programs

As I mentioned previously. Java can be used to create two types of programs: applets and stand-alone applications. An Applet is simply a part of a Web page, just as an image or a line of text can be. Just as a browser takes care of displaying an image referenced in an HTML document, a Java-enabled browser locates and runs an Applet . When your Java-capable Web browser loads the HTML document, the Java applet is also loaded and executed.

Using applets, you can do everything from adding animated graphics to your Web pages to creating complete games and utilities that can be executed over the Internet. Some applets that have already been created with Java include Bar Chart, which embeds a configurable bar chart in an HTML document; Crossword Puzzle, which enables users to solve a crossword puzzle on the Web; and LED Sign, which presents a scrolling, computerized message to viewers of the Web page within which the applet is embedded. Figure 1.3 shows a spreadsheet applet running in Netscape Navigator 2.0.

Figure 1.3 : Applets are small programs that are run from within an HTML document.

Although most Java programmers are excited by the ability to create applets, Java can also be used to create stand-alone applications-that is, applications that don't need to be embedded in an HTML document. The most well-known application is the HotJava Web browser itself, shown in Figure 1.4. This basic browser is completely written in the Java language, showing how Java handles not only normal programming tasks such as looping and evaluating mathematical expressions, but also how it can handle the complexities of telecommunications programming.

Figure 1.4 : The HotJava Web browser is written entirely in the Java programming language.

The Java Developer's Kit

Java is actually more than a computer language; it's also a programming environment that includes a complete set of programming tools. These tools include a compiler, an interpreter, a debugger, a disassembler, a profiler, and more. To create a Java program, you first use a text editor to create the source-code file. You write the source code, of course, in the Java language. After completing the source code, which is always saved with a .java file extension, you compile the program into its byte-code format, the file for which has the .class file extension. It is the .class file that the interpreter loads and executes. Because the byte-code files are fully portable between operating systems, they can be executed on any system that has a Java interpreter.

NOTE
Note that many of Java's tools require long file names, especially the long extensions .java and .class. Both Windows 95 and Windows NT allow these long file names, even under DOS sessions.

After compiling and running a Java program, you may discover that the source code needs modification. The Java debugger can help you find your errors, whereas the Java profiler provides handy information about your program. If you run into a compiled Java program that you'd like to see in source-code form, the Java disassembler will do the translation for you. Java also includes a program that creates the files you need to take advantage of native methods (functions written in another language, such as C++). There's even a program that can create HTML documents from Java source-code files. Although all the development tools are DOS applications-that is, they don't run under Windows-they provide a complete environment for creating and managing Java projects.

If you're a little confused about how the many Java programs work together, don't worry about it. You'll get a chance to learn more about the Java tools as you work through this book. At this point, just be aware that Java provides everything you need to create your own applets and stand-alone applications. In the second part of this book, you'll start learning the Java language, and in the third part, you'll start using Java's tools to create your own applets.

Where Is Java?

All this talk about Java doesn't do you much good until you get your own copy of the Java Development Kit (JDK). You'll probably also want a copy of HotJava and Netscape Navigator 2.0, so that you can try out the Web pages you create with your Java applets. Two versions of the JDK are included on this book's CD-ROM. While version 1.0.1 has been used for many of the test applets, version 1.0.2 represents Sun's latest installment of the JDK at the time of this book's completion. However, because the language is constantly being refined (and was only recently released for Windows), there may have been a newer version released since this book was written.

If you'd like to be sure you have the latest version, you can find HotJava and the JDK on the Sun Microsystems site, located at http://www.sun.com. Once you connect up to Sun Microsystems' home page, navigate to the page shown in Figure 1.5, which provides access to tons of information about Java and enables you to download the files you need. (Of course, Web pages constantly change. Sun Microsystem's site may have changed significantly by the time you read this.)

Figure 1.5 : You can download HotJava and the Java Development Kit from Sun Microsystems' Web pages.

If you want to try out a copy of Netscape Navigator 2.0, hop onto the ol' WWW and go to http://www.netscape.com, which is Netscape's home page. From there, you can find your way to the page shown in Figure 1.6. From this page, you can download any of the software that Netscape has up on the Web.

Figure 1.6 : You can get Netscape Navigator 2.0 from Netscape's WWW site.

NOTE
You might also want to find your way to http://www.microsoft.com, where you can find information about Microsoft's new Web browser, Internet Explorer 3, which features Java support.

Example: Installing HotJava

The HotJava browser is contained in a self-extracting compressed file, which you may download from Sun's Web site at http://java.sun.com. After you extract HotJava's many files (there's a ton of them!) by double-clicking the file, the extraction program uncompresses the files and stores them in a folder named HOTJAVA. Once the files are extracted, you can copy the folder anywhere you like on your hard disk.

When you examine the HOTJAVA folder, you'll see the directory structure shown in Figure 1.7. The BIN folder contains the main HotJava application (HOTJAVA.EXE), as well as many of the Java environment's tools, including the compiler, interpreter, and profiler. The LIB folder contains a number of other files needed for HotJava:

Figure 1.7 : After extraction, the HOTJAVA directory will contain all of HotJava's files.

lib/classes.zip.
This file is needed by the compiler and interpreter, so do not unzip it.
lib/properties/.
This is the template and system defaults for the HotJava properties file.
lib/hotjava/ and
lib/templates/.
These are customizable textual elements and HTML.

NOTE
Documentation for HotJava must be accessed online from the program's Help menu.

Example: Installing the JDK

Although the compressed HotJava file contains many of the Java environment's programs such as the compiler and the interpreter, the Java Developer's Kit contains the complete development suite, including some files not packaged with HotJava. For example, the HotJava compressed file doesn't include the Java debugger or the Appletviewer application, which enables you to view applets without having to load them into a Web browser. The JDK also comes packaged with over 20 sample applets. More importantly, the JDK contains the most up-to-date documentation and tools.

You can find the JDK on this book's CD-ROM, in the JDK folder, or you can download the latest version of the JDK from the Sun Microsystems Web site. On the CD-ROM, the compressed file that contains the JDK is called JDK.EXE. After copying the file to your hard disk (or after downloading the JDK from Sun), you install it in exactly the same way you installed the HotJava browser. That is, you double-click the self-extracting compressed file.

When the files have been extracted (it might take a while), you can move the resultant JAVA folder anywhere you like on your hard disk, although you'll probably want it on the disk's root directory, which is where this book's forthcoming examples will assume it's located. Figure 1.8 shows the contents of the JAVA folder (at least, the contents at the time of this writing; the development kit may have been revised by the time you read this).

Figure 1.8 : The Java Developer's Kit will be in the JAVA folder after you extract its compressed files.

If you examine the folders that make up the JDK, you'll see that the BIN folder holds the developer tools, such as the compiler, the interpreter, the debugger, and the profiler. The DEMO folder contains the many example applets that you can examine to learn more about the Java language and how it's used. In your JAVA folder, you'll also find a file called SRC.ZIP. This compressed file contains the source code for the classes included with the JDK.

Summary

Java just may be the biggest thing to hit the World Wide Web since, well, the Web itself. As you'll see in the forthcoming chapters, Java not only provides a way to create secure applications that can be used safely on the Internet, but the language also represents a complete shift in the way people may think about their computers in the future. Because a Java applet can be located anywhere on the Web yet still be executable on your computer, your computer's storage may well expand from its tiny hard drive to include all of the Internet. As Java becomes more refined, and as more people like yourself start writing Java applets and applications, the cyber world that is the Internet may never be the same.