Java(TM): Programming for the Internet

Karl Jeacle

December 1995

If this is the first time you've come across Java, I can guarantee you, it won't be the last. Over the next twelve months, just about every Internet related publication you read will have something to say about Java.

Sun Microsystem's Java programming language and HotJava(TM) WWW browser are taking the Internet by storm. Since its release in April 1995, Java has started breathing new life into the World Wide Web, which from a user perspective, has remained largely static in technology terms over the last few years.

So what is it?

Java is a programming language. HotJava is a Web browser. HotJava is just like any other Web browser except it can display HTML pages which contain not just text and images, but small applications, or "applets". This "executable content" runs on the Web page. HotJava first downloads a page's HTML file, formats it, inserts any images, and then starts running any applets present on the page. These applets can be as simple as a small animation or audio player "live" on a page, or as complex as any word processor or spreadsheet. Java applets know no bounds.

HotJava is a called a dynamic browser; for a number of reasons. In addition to the interactive content described above, its late binding system offers dynamic types and dynamic protocols.

While most browsers are monolithic programs, with support for HTML, HTTP, and GIF all compiled in, HotJava is dynamic in nature. If a conventional browser has no support for something like JPEG, a new release of the browser is required. With HotJava, if new formats appear, the browser downloads the JPEG file from a Web server, realises it doesn't understand the type, and queries the Web server for code to handle the JPEG file.

The same scenario applies when dealing with new protocols. HTTP is, at present, the most common protocol used on the Web. New protocols are being developed which are more secure, which can be used for Internet commerce. The drawback with these new protocols is that both client browser and Web server software must be aware of them. So if Bank A decides to use Company X's secure Web server, and Bank B decides to use Company Y's secure Web server, both speaking their own proprietary secure versions of HTTP, customers will have to use two different browsers. With HotJava, a new protocol can be added dynamically in the same way as with dynamic types.

History

The origins of the Java project stemmed from a desire by Sun to build advanced software for a wide variety of networked devices, embedded systems, and even consumer electronics devices. The goal was to build a small, distributed, real-time operating environment. C++ was the original language of choice, but as time went by, the difficulties encountered using C++ for the project grew to the point where it made more sense to develop a whole new language, than continue with C++. Hence Java.

If you're wondering how the name Java came about, well, join the club. There have been a lot of stories on the Net about where the name came from. About the most reliable I've come across so far is from Tim Lindholm of Sun Microsystems, who commented on a Usenet news group:

"No, Java is not an acronym. It was come up with in a room full of frustrated people trying to find something that sounded OK and hadn't been trademarked already. The original name of the language (Oak) was owned by someone else and could not be used."

Java: The language

In Sun's White Paper on Java, they use a set of buzzwords to describe Java. However, they then go on to explain each of these one by one in order to present a picture of the problems they were trying to solve when designing Java. I'll try to summarize this, but first, the buzzwords:

Java: A simple, object-oriented, distributed, interpreted, robust, secure, architecture neutral, portable, high-performance, multithreaded, and dynamic language.

Programming languages are getting larger, more feature-rich, and hence more complex and difficult to use. Java is simple, it's small. Start with C++, remove a bunch of rarely used complicated features, add things like automatic garbage collection (no more malloc() and free()), and you've got simple and OO. You've also got small. A self-contained Java microkernel can be built in less than 220 kilobytes.

Distribution: Java provides libraries with TCP code for handling protocols like HTTP and FTP. Java is intended for writing programs that will be reliable. A lot of emphasis goes on early checking for potential problems and avoiding situations which are error prone. One example of this is Java's pointer model. No more pointer arithmetic, Java has true arrays. Programmers can worry less about corrupting memory. This helps make Java Robust.

Since Java is intended for use in a networked/distributed environment, a lot of emphasis has been put on security. The pointer model described above does not allow applications to gain access to object data structures to which they have no access. Combined with public-key encryption techniques, this helps the robustness and secureness of Java.

Networked environments contain lots of different types of equipment. To build a system which can be used to develop code which runs on all of these, it's got to be both architecture neutral and portable. Java applets will run on anything which has a port of the Java engine. Once you've got this engine, it runs your applets by interpreting the bytescodes created by the Java compiler. Java is interpreted.

Despite the interpreted nature of Java, Sun have built the bytecode system so as to gain as high a performance run-time system as possible. To increase interactive responsiveness and real-time behaviour, support for threads has been built into Java. In a world where many different events are happening simultaneously, no longer does the programmer have to link in third party threads packages to handle multiple simultaneous task. Java inherently supports multithreading.

Finally, Java is a dynamic language. It was designed to adapt to an evolving environment. By using a concept known as interfaces, and by binding software component objects together as late as possible, Java avoids a number of software reusability problems evident with languages such as C++, and makes the use of the OO paradigm much more of a reality.

Using Java

Programming Java is a lot like C++. If you can program in C++, you should feel comfortable programming Java within a few days. If you only know C, things are a little harder, or at least, unfamiliar. Once you get used to thinking in terms of objects, and working with object-oriented techniques, you should be fine - you don't need to learn C++ first.

As an example, take a look at the classic "Hello World" program written in Java:

import browser.Applet;
import awt.Graphics;
class HelloWorld extends Applet {
        public void init() {
                resize(150, 25);
        }
        public void paint(Graphics g) {
                g.drawString("Hello world!", 50, 25);
        }
}

This code creates a program, or applet in Java terms, which is 150 by 25 pixels in size, and paints a message at co-ordinates (50,25). Every applet has an init() routine which is called when the applet is created. The paint() routine is called whenever a redraw or refresh is required. In this applet, it always prints the same thing, however, if there was a slider in the applet which the user could vary some value as user input, the paint() routine could be used to dynamically calculate the applet display to reflect the position of the slider. I've over-simplified things here a little, but you should get the general idea.

To get this applet up and running on your Web page, you would save it from your editor as "hello.java", and run the code through the Java compiler by typing something like "javac hello.java". The compiler then outputs a file called "hello.class" which is the binary bytecode file. You're all set. In the same way as there's a HTML tag for including pictures (< IMG SRC="foo.gif" >), there's a tag to place applets in your code. This is either APP or APPLET, depending on which release of Java you're using.

Platforms supported

The initial version of HotJava ran only on Sun SPARCstations running Solaris 2.x. Since then, Sun have released HotJava for Windows NT and Windows 95. This version of the software is called the 1.0Alpha3 release. Since the goal of the Alpha release was to demonstrate the concept of executable content, and to allow developers become familiar with Java, the current version of HotJava is also the last. Work on Java itself has already moved on to a 1.0Pre-Beta release. New third-party browsers will all use this new standard.

Netscape Communications Corporation have announced that they have licensed the Java technology from Sun and their WWW browser Netscape Navigator 2.0 will include support for Java applets. In principle, this means Java could be available for Windows 3.1, Windows 95, Windows NT, Macintosh System 7.5, and a number of Unix variants. At the time of writing, Java support was only present in Sun Solaris and Silicon Graphics IRIX versions.

I know what you're thinking: this all sounds great, but I didn't see the Amiga mentioned anywhere up there, so can I get it for my Amiga? Well, unfortunately, the answer is no; at least, not yet. If you want to be able to use Java on your Amiga, there are a number of possible solutions:

Further Information

Hopefully this article will have stimulated your curiosity enough to look for further information on Java. The best place to start is Sun's official Java web site ( http://java.sun.com/). From here you can find just about everything you need to know. Two other resources worth mentioning are the recently created Usenet newsgroup comp.lang.java, and Gamelon ( http://www.gamelan.com/ ), a directory and registry of Java applets and resources available on the Web. And finally, if you're looking for a good example of a Java-powered Web page, Sun Microsystem's home page always impresses; without Java, it's a nice colour picture; with Java - it's alive!

Conclusion

When I first encountered the Web in 1993, I was hooked. It was one of the most amazing yet simple ideas I had ever come across. Two years later, and my interest was waning. Sure the Web was a great resource, but surfing had lost something of its magical quality.

My first impression on encountering HotJava, like many others, was "Wow!". I strongly encourage you to see a demo of a Java-capable browser in action. For despite my enthusiasm in this article, reading about it is no substitute for seeing it. Believe me, you won't be disappointed.


Java and HotJava are trademarks of Sun Microsystems, Inc.