Welcome to Teach Yourself Café in 21 Days. For the next three weeks, you'll learn all about the Java language and how to use it to create applets and programs. More importantly you also will learn how to use Symantec Café with Java to develop Java programs more quickly and effectively.
An applet is a dynamic and interactive program that can run inside a Web page displayed by a Java-capable browser such as HotJava, Netscape Navigator 2.0 or later, or Microsoft Internet Explorer 3.0.
The HotJava browser is a World Wide Web browser used to view Web pages, follow links, and submit forms. It also can download and play Java applets.
Today you'll learn about the following:
Java is an object-oriented programming language developed by Sun Microsystems, a company best known for its high-end UNIX workstations. Modeled after C++, the Java language was designed to be small, simple, and portable across platforms and operating systems, both at the source and at the binary level (more about this later).
JavaScript is a scripting language created by Netscape to be used in Netscape 2.0 or later. It is a non-object based Java programming language. Instead of compiling JavaScript, you merely include the code with the HTML code in your Web page. The advantages of JavaScript is that because it is a simplified version of Java, it is easier to learn, use, and implement. The disadvantage of using JavaScript is that your code is openly available to anyone viewing your Web page. (Java applets require you to compile your code into a binary file.) Also, JavaScript can only be used with Netscape Navigator 2.0 or later, thus limiting the audience that can view your JavaScripts. Symantec Café is based on Java only and lets you develop full-fledged Java applets as well as stand-alone applications. If you want information on learning to code JavaScript, check out the book Teach Yourself JavaScript in a Week, also by Sams.net Publishing.
Just like Netscape Navigator 2.0 (see Figure 1.1), HotJava is a World Wide Web browser from Sun. HotJava is Sun's version of a Java-compliant browser. Applets appear in a Web page much in the same way images do, but unlike images, applets are dynamic and interactive. Applets can be used to create animations, figures, areas that can respond to input from the reader, games, or other interactive effects on the same Web pages among the text and graphics.
Figure 1.1 : The Netscape browser.
Although HotJava was the first World Wide Web browser that could play Java applets, Java support is available on other browsers as well. Netscape Navigator 2.0 or later provides support for Java applets, and Microsoft's Internet Explorer 3.0 also supports Java applets.
To create an applet, you write it in the Java language, compile it using a Java-based compiler, and refer to that applet in your HTML Web pages. You put the resulting HTML and Java files on a Web site much in the same way that you make ordinary HTML and image files available. Then, when someone using a Java-enabled browser views your page containing the embedded applet, that browser downloads the applet to the local system and executes it, and the user then can view and interact with your applet in all its glory. People using other browsers (non-Java capable browsers) might see text explaining that an applet is designated to have loaded here, a static graphic representing what the Java applet was meant to do, or nothing at all. You'll learn more about how applets, browsers, and the World Wide Web work together later on in this book. The following section gives you an introduction to how Java applets are embedded in HTML pages.
To include an applet on a Web page, you refer to that applet in the HTML code for that Web page. Imagine that you have an applet that will display the words "Hello World" when loaded. This fictitious applet will be called HelloWorldApplet.class. All compiled Java applets end with the extension .class. The following is a very simple HTML file:
<HTML>
<HEAD>
<TITLE>Hello to Everyone!</TITLE>
</HEAD><BODY>
<P>My Java applet says: </P>
<APPLET CODE="HelloWorldApplet.class" WIDTH=150 HEIGHT=25></APPLET>
</BODY>
</HTML>
You refer to an applet in your HTML files with the <APPLET> tag. You'll learn more about <APPLET> later on, but here are two things to note:
Now you're ready forOhe final test-actually viewing the result of your applet. To view the applet, you need one of the following:
| Note |
Do not use the alpha version of HotJava to view your applets. Applets developed with the beta Java language specifications and onward cannot be viewed by the alpha version of HotJava. By the time you read this there will most likely be a more recent version of HotJava; make sure you use that one instead. |
If you are using a Java-capable browser such as Netscape Navigator 2.0 to view your applets, you can use the Open Local... item under the File menu to navigate to the HTML file containing the applet (make sure that you open the HTML file and not the class file). In this example there is no need to install anything on a Web server; everything will work on your local system.
Now, if you use the browser to view the applet, you will see something similar to the image shown in Figure 1.2.
Figure 1.2 : HelloWorldApplet.
The important thing to understand about Java is that you can do much more with it than create applets. Java is a fully functional object-oriented programming language in which you can accomplish the same sorts of tasks and solve the same sorts of problems that you can in other programming languages, such as C or C++.
Java programs fall into two main groups: applets and applications.
Applets, as you have learned, are Java programs that are downloaded over the World Wide Web and executed by a Web browser on the user's machine. Applets depend on a Java-capable browser in order to run (although they also can be viewed using a tool called the appletviewer, which you'll learn about tomorrow).
Java applications are more general programs written in the Java language. Java applications don't require a browser to run, and in fact, Java can be used to create all of the kinds of applications that you normally would use a more conventional programming language to create. HotJava itself, including all of the networking, display, and user interface elements, is a Java application.
A single Java program can be an applet, an application, or both-depending on how you write the program and the capabilities that the program uses. Throughout this book you will have an opportunity to work with developing applications and applets in Java.
The Java language was developed at Sun Microsystems in 1991 as part of a research project to develop software for consumer electronics devices-television sets, VCRs, toasters, and the other sorts of machines you can buy at any department store. Java's goals at that time were to be small, fast, efficient, and easily portable to a wide range of hardware devices. It is those same goals that made Java an ideal language for distributing executable programs through the World Wide Web, and also a general-purpose programming language for developing programs that are easily usable and portable across different platforms.
The Java language was used in several projects within Sun, but it did not get very much commercial attention until it was paired with HotJava. HotJava was written in 1994 in a matter of months, both as a vehicle for downloading and running applets and also as an example of the sort of complex application that can be written in Java.
The Java Development Kit (JDK) is a command-line based development environment for Java. It unfortunately is not very user friendly because it is based entirely on a set of command-line tools. While the JDK was the first tool for developing Java applications, it is certainly not the only available tool to develop Java applets. The Java Development Kit can be used on Sun systems running Solaris 2.3 or higher, Windows NT, Windows 95, and now the Macintosh. All of the examples and code in this book are designed to work with any Java development environment that supports 1.0 Java language specifications.
As stated, you'll need a Java-capable browser or other tool to run and view Java applets. Netscape Navigator 2.0 or later provides Java capabilities for most platforms. As of this writing, however, Sun's HotJava does not support applets written under JDK 1.0. A newer version of the HotJava browser should be available in the near future to resolve this issue, but this book will use Navigator because it is currently the market-leading Java-capable browser.
Sun's JDK 1.0 and Symantec Café both include an application called appletviewer that allows you to test your Java applets as you write them. If an applet works in the appletviewer, it should work with any Java-capable browser. You'll learn more about appletviewer later today.
What's in store for Java's future? Much, because Sun is no longer the only major company coming up with great ideas using Java. Novell, IBM, Microsoft, Borland, Netscape, and Symantec are just a few companies that have licensed the Java technology from Sun. Typically there are two types of tools coming out from most of these companies: browser-related Java tools or development-related Java tools. For example, Netscape Navigator 2.0 not only gives its users the ability to view Java applets, but also has added support for JavaScript, a non- object-based scripting language that can be used on Netscape browsers. Other companies have bundled Java tools with their C++ products, such as Borland in its C++ 5.0. More importantly, companies such as Symantec have created tools known as IDEs (integrated development environments) that specifically are designed for Java development.
An integrated development environment is a program that is made up of several separate development tools. In essence, each of these tools works together throughout the development cycle. Symantec Café is a perfect real-world example of an IDE for Java.
At the moment, probably the most compelling reason to learn Java is because Java is one of the biggest buzzwords in the Internet. Even if that was not the case, Java as a language has significant advantages over other languages and other programming environments that make it suitable for many other programming tasks. This section describes some of those advantages.
Platform independence is one of the most significant advantages that Java has over other programming languages, particularly for systems that need to work on many different platforms. Java is platform-independent at both the source and binary levels. For example, a Java program developed on a pc can be executed on a Macintosh (and vice versa) with no changes made to the source code and no need to recompile the program either.
Platform independence is a program's capability of moving easily from one computer system to another.
At the source code level, Java's primitive data types have consistent sizes across all development platforms. Java's class libraries make it easy to write code that can be moved from platform to platform without the need to rewrite it to work with each platform.
Platform independence doesn't stop at the source level, however. Java binary files (or executables) are also platform-independent and can run on multiple platforms without the need to recompile the source. How does this work? Java binary files are actually stored in a form called bytecodes.
Bytecodes are a set of instructions that look a lot like machine code, but are not specific to any one processor.
Normally, when you compile a program written in C, C++, or most other languages, the compiler translates your program into machine codes or processor instructions. Those instructions are specific to the processor your computer is running-so for example, if you compile your code on a Pentium system, the resulting program is optimized to run only on pc-based systems, and will not run on non-pc-based processors. If you want to use the same program on another system, you have to go back to your original source code, get a compiler for that system, and recompile your code. Figure 1.3 shows the result of this system: multiple executable programs for multiple systems.
Figure 1.3 : Traditional compiled programs.
Things are different when you write code in Java. The Java development environment has two parts: a Java compiler and a Java interpreter. The Java compiler takes your Java program, and instead of generating machine codes from your source files, it generates bytecodes.
To run a Java program, you run a program called a bytecode interpreter, which in turn executes your Java program (see Figure 1.4). You can either run the interpreter by itself, or for applets there is a bytecode interpreter (built into Netscape Navigator and other Java-capable browsers) that runs the applet.
Why go through the trouble of adding this extra layer of a bytecode interpreter? Having your Java programs in bytecode form means that instead of being specific to any system, your programs can be run on any platform and any operating system as long as the Java interpreter is available. This capability for a single binary file to be executable across platforms is crucial to what enables applets to work because the World Wide Web itself is also platform-independent. Just as HTML files can be read on any platform, applets can be executed on any platform that is a Java-capable browser.
The disadvantage of using bytecodes is execution speed. Because system-specific programs run directly on the hardware that they are compiled for, they run significantly faster than Java bytecodes, which must be processed by the interpreter. For many Java programs, the speed might not be an issue. If you write programs that require more execution speed than the Java interpreter can provide, you have several solutions available to you, including the ability to link native code into your Java program, or to use tools to convert your Java bytecodes into native code. Note that by using any of these solutions, you lose the portability that Java bytecodes provide. Another solution that may end the speed versus versatility dilemma is a Just in Time compiler that keeps Java portable-without compromising speed. You'll learn about each of these mechanisms during Week 3.
To some, object-oriented programming (OOP) is merely a way to organize programs, and it can be accomplished using any language. Working with a real object-oriented language and programming environment, however, enables you to take full advantage of object-oriented methodology and its capabilities for creating flexible, modular programs and reusing code.
Many of Java's object-oriented concepts are inherited from C++, the language on which it is based, but it borrows many concepts from other object-oriented languages as well. Like most object-oriented programming languages, Java includes a set of class libraries that provide basic data types, system input and output capabilities, and other utility functions. These basic classes are part of the Java Development Kit, which also has classes to support networking, common Internet protocols, and user interface toolkit functions. Because these class libraries are written in Java, they are portable across platforms.
You'll learn more about object-oriented programming and Java on Day 3.
In addition to its portability and object-orientation, one of Java's initial design goals was to be simple and small. Keeping the language small makes it more robust because there are fewer chances for programmers to make difficult-to-find mistakes. Despite its size and simple design, however, Java still has a great deal of power and flexibility.
Java is modeled after C and C++, and much of the syntax and object-oriented structure is borrowed from the latter. If you are familiar with C++, learning Java will be particularly easy for you, because you have most of the foundation already.
Although Java looks similar to C and C++, most of the more complex parts of those languages have been excluded from Java, making the language simpler without sacrificing much of its power. There are no pointers in Java, nor is there pointer arithmetic. Strings and arrays are real objects in Java. Memory management is automatic. To an experienced programmer, these omissions might be difficult to get used to, but to beginners or programmers who have worked in other languages, they make the Java language far easier to learn.
One of the most intimidating things about Java when it was first released was the lack of "modern" integrated development environments to write and compile Java programs with. The Java Development Kit was the first and probably the most widespread development environment available for Java. It is merely a set of command-line tools with which programs are authored using any text editor that can save in ASCII format. Source files are compiled and debugged using command-line executables provided with the Java Development Kit. With current development environments, it is unlikely that you will see many developers building programs in any language from a text editor. As a result, this caused Java to become very intimidating. This book, however, will go beyond giving you a detailed tour of Java. It also will discuss how to take advantage of Café, Symantec's new tool for developing in Java.
An IDE gives you the ability to develop programs more efficiently. IDEs are designed to take the entire development process and condense it into one tightly bound program. IDEs are also known to have wizard-like tools that do routine tasks for you. A typical wizard could have the capability of writing skeleton code for your Java projects. IDEs are more than that with powerful tools like visual debuggers and viewers that give you the ability to point and click your way through your programs. Café is the next step in bringing Java to the plate as a real programming language because Café is designed around the Java Development Kit and Java. Symantec Café contains wizard-like tools (called Express Agents) to perform routine tasks, visual tools that give you a pictorial view of your Java programs, and a visual debugger to help you debug your code.
Symantec calls its product Café an IDDE (or integrated development and debugging environment). This really is the same thing as an IDE (integrated development environment). Café is designed to be the next step up from Symantec's previous Java development environment, Espresso.
| Note |
Espresso is Symantec's wrapper utility for Symantec C++ that is designed to give owners of Symantec C++ the ability to develop Java applications. It is being replaced by Symantec Café, a fully functional stand-alone Java Integrated Development Environment. |
By the end of this book you should be able to appreciate the Symantec Café as a very powerful tool for Java development. Café allows you to create Java applets and embed them into Web pages. It also links with your existing default Web browser (if you have one installed) to view these pages, and you can use the built-in appletviewer to view them as well.
Symantec Café contains the full object-oriented Java functionality
to create stand-alone Java programs. Symantec Café has
provided substantial enhancements to the original
Java Development Kit. However, Symantec has based every change
it has made on the Java Development Kit. Symantec has not made
any changes to the built-in class libraries that come with the
Java Development Kit, but Symantec has developed a class viewer
that lets you navigate these built-in class libraries with your
mouse. Figure 1.5 shows an example of what Symantec Café
looks like.
In order to write the Java programs that are described in this book, you will need to have Symantec Café installed on your system. The software should be available from your local software store, and for more information you can visit Symantec's home page at the following address:
http://cafe.symantec.com
| Note |
Symantec Café discussed in this book is designed for the Windows NT and Windows 95 operating systems and is currently available in version 1.2. A Symantec Café for the Macintosh is also available. |
Although Netscape and other Java-enabled browsers provide an environment for running Java applets, they do not provide a mechanism for developing new Java applets. For that, you need separate tools-merely having a browser is not enough. That is why you need a tool such as Symantec Café to help you to develop both Java applets and applications.
Today, you received a basic introduction to the Java language and its goals and features. Java is a programming language similar to C or C++ that you can use to develop a wide range of programs. The most common use of Java at the moment is to create applets that can be used with Netscape Navigator, an advanced World Wide Web browser. Applets are Java programs that are downloaded and run as part of a Web page. Applets can be used to create animations, games, interactive programs, and other multimedia effects on Web pages.
Java's strengths lie in its portability-both at the source and at the binary level in its object-oriented design-and in its simplicity. Each of these features helps to make applets possible, but also makes Java an excellent language for writing more general-purpose programs that do not require HotJava or another Java-capable browser to run. These general-purpose Java programs are called applications. HotJava itself is a Java application.
To end this day, you experimented with an example applet and an example application, getting a feel for the differences between the two and how to create, compile, and run Java programs-or in the case of applets, how to include them in Web pages. Tomorrow you will have a chance to compile your first project using Symantec Café.
| What browser should I use to view my Java applets? | |
| The focus of this book is primarily on programming in Java and its library of standard classes. Furthermore, Café comes with the appletviewer tool that lets you view applets in a pseudo-browser environment. This tool is part of the Café desktop and is primarily what will be used to test Java applets throughout the rest of this book. As a result, it is irrelevant what Internet browser you decide to view your applets provided that the browser is Java capable. | |
| I know a lot about HTML, but not much about computer programming. Can I still write Java programs? | |
| If you have no programming experience whatsoever, you most likely will find programming Java significantly more difficult. However, Java is an excellent language to learn programming with, and if you patiently work through the examples and the exercises in this book, you should be able to learn enough to get started with Java. | |
| According to today's lesson, Java applets are downloaded using a Java-enabled browser such as HotJava or Netscape Navigator 2.0 or later and run on the reader's system. Isn't that an enormous security hole? What stops someone from writing an applet that compromises the security of my system-or worse, an applet that damages my system? | |
| Sun's Java team has thought a great deal about the security of applets within Java-capable browsers and has implemented the following checks to make sure applets cannot do nasty things: | |
| |
| |
| |
| Note that some of these restrictions might be allowed in some browsers or might be turned on in the browser configuration. However, you cannot expect any of these capabilities to be available. | |
| In addition, the Java compiler and interpreter check both the Java source code and the Java bytecodes to make sure that the Java programmer has not tried any sneaky tricks (for example, overrunning buffers or stack frames). | |
| These checks obviously cannot stop every potential security hole (no system can promise that!), but they can significantly reduce the potential for hostile applets. You'll learn more about security issues later on in this book. | |
| Why use Symantec Café? | |
| While a detailed explanation of each all the major development environments is beyond the scope of this answer, I'll give you the thumbnail version. Symantec Café is the first official Java IDE to be released. It is designed around the object-oriented nature of Java, and uses much faster tools to work with than those included with the Java Development Kit. Café has a well-rounded IDE with a full set of help files on how to use Café and how to reference material on Java. Using Café will make you a better Java developer because you will be writing less redundant code, writing fewer errors, and spending less time waiting for your Java projects to compile. |