Java Programming ... From the Grounds Up
by Mark C. Reynolds
Reprinted from Web Developer® magazine, Vol. 2 No.1 Spring 1996 © 1996
With Java, it's possible to write some very sophisticated applets with a relatively small amount of code. Here's how.
Wildly popular due to its interactive multimedia capabilities, Java programming leads the list of Internet development skills in current commercial demand. In this first half of our two-part tutorial on Java applet development, we explore the essentials of Java's components. These include how Java development tools relate to each other and--most importantly--how they are used to provide content that executes on the client side instead of on your server.
Before Sun Microsystems introduced Java, most Web interactivity was accomplished via CGI (Common Gateway Interface) scripting. This is frequently utilized in forms or guestbooks where users type entries into text fields, then submit this information via their browser back to a host server. The host server then passes the information to an external program running on the Web server's machine. The output of this external program is then passed from the server back to the browser. CGIs must execute at least one round trip from the browser to the server and back.
In contrast, when a Java-compatible browser accesses a Java-powered page, an applet--a small program written in Java--is copied to the browser's machine and executes there. It does not execute on the server the way a CGI program does. This local execution makes possible a much greater level of Web interaction and multimedia effects, unhampered by HTML restrictions or network bandwidth. Java programs can display slick animations, invite users to play games, show step-by-step tutorial instructions, or run demonstration versions of computer software. When a browser accesses a standard HTML page, the result is usually a static display. When it runs a Java applet, however, the results are limited only by the creativity of the applet's developer. The applet is a nearly independent application running within the browser.
Java is an object-oriented programming language that resembles a simplified form of C++. Java code displays graphics, accesses the network, and interfaces with users via a set of capabilities--known as classes--that define similar states and common methods for an object's behavior. Unlike other programming languages, though, Java programs are not compiled into machine code; instead, they are converted into an architecture-neutral bytecode format. This collection of bytes represents the code for an abstract Java virtual machine (VM). In order for these bytes to execute on a physical machine, a Java interpreter running on that physical machine must translate those bytes into local actions, such as printing a string or drawing a button.
To run Java applets, you'll need a Java-enabled browser (such as Sun's HotJava, Netscape 2.0 or greater, or Internet Explorer 3.0 or greater) or you can use the appletviewer utility in Sun's Java Development Kit (JDK). The JDK also includes an interpreter for standalone Java applications (called simply java), as well as a debugger and compiler, called jdbg and javac respectively.
Java applets use a subset of the full Java VM, with a variety of features disabled for security reasons. You can add Java applets to your Web pages with the <APPLET> tag, which can include attributes for the applet's height, width, and other parameters. Java-capable browsers treat Java applets like other media objects in an HTML document: they are loaded with the rest of the page, then verified and executed.
Java Classes and Methods
Java utilizes the basic object technology found in C++. In a nutshell, the Java language supports the idea of data packaging, or encapsulation, through its mechanism. A Java class is an association between data elements and/or functions, much like an extended struct in C (or a C++ class). In fact, there are no structs in Java at all; the mechanism of grouping together similar elements is achieved only by creating a class. The functional members of a class are referred to as the class methods. Just as a C struct may contain other structs within it, a Java class may be built on top of another class--although only one at a time--and inherit that class's behaviors as well.
Java has its own syntax for describing methods and classes. It supports public class members, which are visible outside the class; protected members, which are visible only within the class and its subclasses; and private members, which are only visible within that particular class. Java supports abstract (virtual) classes, in which some or all of the member functions are declared, but not defined--they have no function body, so that only subclasses which fully define those functions may be used.
If you have some experience with C++ programming, many of these concepts will be familiar to you. However, there are several striking differences between C++ and Java. Much of the implicit behavior that C++ takes for granted is absent in Java. For example, there are no default constructors: a Java program must explicitly call the operator new to create a new instance of a class. In addition, arithmetic operators such as "+" or "= =" may not overload in Java. There is no way for the programmer to extend the behavior of "+" beyond what Java provides intrinsically. Another highly visible departure from C and C++ is that there are no pointers (and logically, no pointer arithmetic) in Java.
| [ Java Programming ... From the Grounds Up: |
Part 2 > ]