by David Wood

Java: it's more than just an excuse for a lot of bad puns. Unlike other languages, if you write in this one, it will execute the same on a number of different computer platforms (we promise!), and, better yet, as Netscape has been so kind as to include a Java interpreter into their browser, most of the people surfing the Web (according to Netscape) can see the results of your programming labors without even having to type in the name of your program.

Does it sound like we're being a little facetious? Well, we are; the thing to keep in mind with Java is that it really is just another programming language. In fact, it's just another C++-like, object-oriented programming language. The only thing making it really special is that it seems to have been designed from the start with an eye toward the Web; Sun's gone to great lengths to make it portable, versatile, and (gasp) even efficient. Actually, they have a lot of adjectives that they're proud to use in connection with this language. (Naturally, before we go any further, you should check out their Java site and bookmark it.)

Java programs are "compiled" into a pseudo-machine language instruction format Sun calls bytecodes, and executed in a simulated environment that is implemented individually on a number of platforms. The language allows for some cute syntactic tricks such as method overloading, and strings are given (slightly) better treatment than in C/C++. Mark-sweep garbage collection automatically removes objects no longer in use. The list goes on.

The biggest hurdles when making Java a part of your repertoire are just getting used to the quirks and semantics of a new compiler (javac) and then (this is the big one) getting used to the API. There's a lot to read and a lot to experiment with. But first some cautious explanation.

In case you haven't been following all the hype about the Java project, you might not know to be prepared for the aftermath of a product that's been in wide use since one of its earlier alpha releases. This means that the Java language has actually been changing while its development community was using it. There is frequently confusion between "Alpha"- and "Beta"-based applets and applications. As of the time of this writing, Sun claims to have frozen development of the API. The Java Developer's Kit is "complete" at version 1.0.1; all "Alpha code" has been made obsolete.

What does this mean for you? Just keep in mind (again, at the time of this writing) that Netscape's browsers are written to support the "Beta" (which is synonymous with "final") API. HotJava, Sun's Java-applet-savvy Web browser (a pretty impressive feat by itself), is still in its Alpha phase and does not yet support applets created with the new development kit. Netscape's browser does. Go figure.

There are already several places on the Web where you can go to find a nice tutorial to ease you into writing your first simple Java code, foremost being the one created by Sun itself. You, too, can participate in the age-old tradition of writing a "Hello World" program in a new language, or, if you prefer to defy convention, you can follow our abridged guide to writing a first applet that does not say anything so insipid.


import java.awt.Graphics;
import java.awt.Color;

public class FirstTry extends java.applet.Applet {

   StringBuffer buffer = new StringBuffer("Click Me!");

   public void init() {
      resize (150,25);
   }

   public boolean mouseDown (java.awt.Event evt,int x,int y) {
      buffer = new StringBuffer("Thank You!");
      repaint();
      return(false);
   }

   public void paint(Graphics g) {
      g.setColor(Color.darkGray);
      g.draw3DRect(0,0,149,24,true);
      g.setColor(Color.black);
      g.drawString(buffer.toString(),50,18);
   }
}

This is a most primitive example of the real thing, with a few interesting features for show. To be fair, this isn't so much a guide but a set of examples to get you started. We'll cover more specific details in the coming weeks. Briefly, though, there are several significant features about this applet you should see. We "import" two classes for additional functionality. Our Applet is an object that inherits from the Applet class, called FirstTry, and it takes no parameters. It makes use of StringBuffer objects; furthermore, if you were paying attention before when garbage collection was mentioned, you may have noticed that a second buffer was indiscriminately created and assigned to the "buffer" variable, discarding the first. We do this with impunity, knowing that the old "Click Me!" buffer will automatically be deallocated. Close observers will also realize that we're redefining "standard" objects that are called when certain events occur. Thus, a click within the Applet's area of influence on the Web page fires off mouseDown and generates a result:

You can download the Java Developer's Kit, type in this applet, compile it into a ".class" file with javac, and insert it into your HTML documents (using the APPLET tag). You'll find that the compiler expects your source text file to be called FirstTry.java and nothing else. Java, you will find, is apt to force you to do things that (it thinks) will help you to avoid making mistakes later, for better or for worse.

If you're energetic enough to fiddle with the code a little bit (perhaps to change the colors or the strings), you'll notice, as we did, that when using Netscape to test your Applet, you must quit and restart your browser for changes to the FirstTry.class file (your "compiled" product) to take effect.

No doubt, all this has created many more questions than answers for you; this is just what we hoped it would. Our advice is to explore the Java site, look at source code for other applets, experiment indiscriminately, and come back next week to see what the next installment of our saga brings.

http://www.internet.com/