column

Bahasa Java

WEBsmith's New Java Column!

The computer industry is abuzz with talk about Java(tm), a new Internet programming language. Webmasters are asking "What is it, what is it good for, and what do I need to know about it?" As a service to our readers, this new column in WEBsmith will provide ongoing answers to these questions.

by Tim Maher

Why is Java significant? Because it brings animation and interaction to the Web, which has been characterized by some as nothing more than an electronic medium for the exchange of digitized marketing brochures. With Java, web pages become dynamic, interactive multimedia experiences!

The "Net Surfer" can play games, draw pictures, click on responsive maps, take dancing lessons, watch animations, run spreadsheets, and more. The Webmaster can display different text strings in a sideways-scrolling window (emulating the LED panels seen in retail establishments) at different times of the day, or for different visitors, or when different items reach surplus status and need to be cleared from inventory. The background image of the WEB page can be "sensitized" to invoke different HTML files when clicked in different areas. Histograms can be dynamically created and displayed to the Surfer on the basis of his typed input. Animated figures can dance around the page, entertaining the Surfer while background processing takes place. Sure, you can do some or all of this with CGI, TCL/TK, Perl, or AWK if you are creative enough, but this can all be relatively easily done with Java alone!

Grab yourself a Java capable browser (more on this below) and surf * www.gamelan.com, if you haven't done so already, to see what I mean!

Language Overview.

Descriptions of the Java language resound with the latest high-tech buzzwords--and the following is no exception! Java is an object-oriented, interpreted, architecture neutral, multi-threaded programming language with special features optimizing it for distributed, robust, and secure applications--like Web pages!

Because Java is "object-oriented" and equipped with a substantial Application Programming Interface (API), which serves a purpose roughly analogous to that of the Standard Library used by C programmers, many programming tasks can be accomplished by simply "extending" existing pieces of Java code rather than by writing new code from scratch. Moreover, because of the intrinsic "encapsulation" of the resulting "objects", these extensions can be easily incorporated into other programs with impunity (which will be a welcome new experience for C programmers!).

In fact, a particular kind of Java program, called an "applet," is so modular that you can reference one from the existing HTML code of any Web page for automatic activation when a Java capable Web browser encounters the associated tag.

Because Java code is interpreted by the browser program, rather than being compiled for a particular CPU architecture, the code distributed by Web servers does not need to be in the client machine's native execution format. Instead, it is in a partially compiled, but nonetheless architecture neutral "byte-code" format.

The multi-threaded capability of Java programs allows them to follow multiple paths of execution within the context of a single operating system process. This lends significant added functionality to the program in a very efficient manner, and allows for such ergonomically critical effects as sensing mouse or scrollbar clicks with one thread while another is playing an audio clip or loading an image.

Subsequent installments of this column will provide more detailed coverage of some of Java's more significant and complex features. In this introductory article, we'll provide enough background to get you started with incorporating existing applets into your Web pages. We'll also introduce the topic of Java applications, which will be pursued in more detail in a later installment.

About the Name "Java"

What does this language have to do with the largest island of the Indonesian archipelago, or the slang name for coffee? The word from Sun is--not much! The official story is that all the technical sounding language names were already taken, and that the language's previous name, OAK, was found to have been already registered by another party. In frustration, somebody called for java, and the name stuck. Or something like that. But there are whisperings afoot that Java is really a secret acronym for "Just Another Variation on ALGOL 68!" Others say it stands for "Just Another Vague Acronym." Someday the truth will be known. . .

History

The language that evolved into Java was created early in this decade by James Gosling to provide operational control over consumer products. (Does that name sound familiar? He was the author of the original Emacs editor, later enhanced by Richard Stallman of GNU fame.) For a variety of reasons, the standard C and C++ languages used by software developers were not well suited to being embedded within toasters and their ilk, so a new language, whose influences included C++, Objective C, and SmallTalk, was born!

Sun officially announced Java in May, 1995 at the SunWorld conference in San Francisco, and many curious programmers and Websmiths downloaded the free Alpha and Beta versions of the Java Development Kit (JDK) for testing. This means that nobody has been programming in Java for very long, and if you start soon, you'll still be in the first wave!

What You Need and Where to Get It

If all you want to do is view "Java Powered" Web pages, you only need a Java capable browser, such as * Netscape(tm) Navigator 2.0.

If you wish to insert an applet written by somebody else into your Home page (and there are many of these circulating on the Net), and you can get the compiled applet file (the one with the ".class" suffix), and the associated audio and/or image files, all you need do is insert the appropriate <applet> tag into your HTML file (see below).

If the compiled applet files are not available, but the java source files are (the ones with the ".java" suffixes), then you'll need the JDK to generate the .class files, as described below for Applications.

Finally, if you want to write your own Java programs, modify existing ones, or merely compile existing ones, you'll need the JDK (version 1.0 was released in January, 1996), which can be downloaded from *http://java.sun.com (hereafter referred to as "the Java Site").

Applets

Java is a full fledged programming language, whose programs come in two varieties: applets and applications. The fundamental difference is that an applet is a sort of "junior application", designed for invocation by a Web browser, whereas an application is a program that can run on its own.

Many nifty applets are available for viewing and/or downloading from various Web sites, including the Java Site and * the Gamelan site (a "gamelan" is an Indonesian percussion ensemble). Many also come in the JDK package.

For example, you might want to include a message on your Web page whose individual characters wiggle around a bit, to attract the Surfer's attention. This effect has come to be known as "Nervous Text," and it is supported by an applet of the same name that is distributed with the JDK and downloadable from the Java Site. You would place its byte-code file, NervousText.class, in the directory with your HTML file, and add the following tag to the HTML file:

<applet code="NervousText.class" width=150 height=50>
<param name=text  value="Nervous Text">
Non-Nervous Text
</applet>

The width and height parameters, which are accepted by all applets, tell the browser how to size the applet's display region.[footnote 1] The optional <param> tag passes in the string that this particular applet will use as the text to be jiggled around within the applet window. If you omit the <param> line, the applet defaults to jiggling the words "Hot Java" there.

How will you know what "tunable parameters" are accepted by a particular applet? The most accurate technique is to read its source code, and look for invocations of the getParameter() method. Alternatively, you might find documentation in a README file that lists them.

Any text appearing before the closing </applet> tag, such as the "Non-Nervous Text" shown in the example, will be displayed to Surfers whose browsers lack Java empowerment.

Applications

An application is a program that can run in a stand-alone manner (i.e, without the support of a browser). To run an application, you need to have the java interpreter and the API (sometimes called a "class library") installed on the local machine. These items are part of the JDK, downloadable from the Java Site.

Sample applications include tools for managing personal phone directories, proofreading text, and calculating mortgage payments.

The traditional beginner's "Hello World" program is handled like this in Java:

Point shell at Java runtime environment, and directory for custom program:

/home/tim/bin $ CLASSPATH=/local/java:/home/tim/bin

export the variable:

/home/tim/bin $ export CLASSPATH

file created earlier with editor looks like this:

// Application that prints "Hello World" to output
class HelloWorld {
        public static void main (String args[]) {
        System.out.println ("Hello World!");
        }
}

invoke Java Compiler, producing HelloWorld.class

/home/tim/bin $ javac HelloWorld.java

run the Java Interpreter on HelloWorld.class

/home/tim/bin $ java HelloWorld

and you get...

Hello World!

The program should look somewhat familiar to C programmers, and more so to C++ programmers. There's much more to be said about applications, but that's for coverage in a following column.

Licensing

The terms for licensing Java are provided at the Java Site. This is not a legal forum, and I am not an attorney, so you should consult with qualified legal counsel to ensure that your use of Java complies with Sun's licensing terms. My personal reading of the situation is that one can use Java in Web pages without paying licensing fees, although such fees would apply to commercial sales of products that incorporate Java technology. Note also that Sun charges special fees to license some of the Java logos and trademarks, so think twice before including these in your web pages!

Two good places to to watch for Java information are * http://lightyear.ncsa.uiuc.edu/~srp/java/java.html, which provides an exhaustive listing of all current and upcoming Java books, and Sun's new Web-zine called * JavaWorld, which is the new home of the Java Developer column previously found in * SunWorld Online.

Another important Java-related site is * Digital Espresso, which provides the much needed service of summarizing the traffic on the Java Usenet Newsgroup (comp.lang.java).

The Learning Curve

It is extremely easy to really jazz up a Web page by incorporating prefabricated applets. All you need to do is get some applets, learn the <applet> tag, and you're in business.

Programming in Java, on the other hand, is more difficult. Because much can be accomplished by extending existing classes--which is largely accomplished by poking a few carefully chosen keywords in the right places--the act of customizing a program can appear a trivial enterprise to an onlooker.

However, for those approaching Java from a background in procedural languages such as C, or from markup languages such as HTML, the task of choosing the correct keywords will require some degree of mastery over a critical set of highly abstract and powerful object-oriented concepts, such as Polymorphism and Function (Method) Overloading. These concepts take some time to incorporate into your programming mindset.

C++ programmers will already be familiar with object oriented concepts, but to master Java, they have the challenge of forgetting some of what they know (because Java is in some ways a subset of C++), and relearning other parts (because Java uses some of the same keywords and operators, but with different meanings).

But it's an exceptionally well designed language, conspicuously lacking in innumerable creative ways to accidentally shoot yourself in the foot (unlike C), or unwieldy layers of added complexity periodically redefining the base language with different vendors supporting different feature sets (unlike C++).

I personally am tremendously impressed with this language. After being tormented for twenty years by the ruthless precision with which C exposes the programmer's inability to think exactly like a machine (darn those pointers!), chills ran up my spine when I first read through some Java source code, and beheld its beauty! This language has everything necessary to become dominant by the end of the millennium. Which isn't far off! Doesn't this sound like a bandwagon you should be jumping on?


Dr. Tim Maher is the founder and "Head UNIX Guru" of CONSULTIX, a Seattle firm that provides Unix training to the Fortune 500 and U.S. Government. In his spare time, he enjoys biking, boating, gardening, jamming with dead guitar heroes, and teaching tricks to his pet cockatoo, Thor. You can visit his web page (which has a link to Thor's) at: * http://www.consultix.wa.com/yumpy/tj/.

(Java and HotJava are trademarks of Sun Microsystems, Inc., and refer to Sun's Java programming language and HotJava browser technologies. WEBsmith is not sponsored by or affiliated with Sun Microsystems, Inc.)