Java Intro
Volume Number: 12
Issue Number: 3
Column Tag: Internet Tools
Writing Java 102 
Moving beyond “Hello, World” - way, way beyond
By Richard Cardona, TradeWave Corporation
Note: Source code files accompanying article are located on MacTech CD-ROM orsource code disks.
In an attempt to carve a path through the forest of current announcements, discussion,
and interest in Java, let me lay out the components of Java and then proceed to a
discussion in detail [see also MacTech Magazine 12.1 (January, 1996) 51-57 -
man].
Java consists of a programming language, a virtual machine
architecture, and a runtime environment developed by Sun Microsystems. The
most common instance of Java technology is a Web browser running applets with
their own interface. These applets are downloaded in-line on a web page, just like
images. Thus applets can be served by all standard web servers, including those on the
Mac, as just another MIME type.
HotJava was the first Java-enabled Web browser. It was authored by Sun
completely in Java, to prove that complex applications could be written in Java. A
Java-aware browser runs applets with the help of the Java Virtual Machine, an
interpreter of bytecodes produced by a Java compiler. Although bytecodes are
currently interpreted, Sun expects to provide just-in-time compilation to improve
speed and rival native applications.
The most compelling reason for applets is that they let dynamic web media be
targeted for content processing by the client. Unlike today’s Common Gateway
Interface programs (CGI), Java applets execute on the client machine in the browser.
This relieves the burden on many servers that are far too busy already, and reduces
server cost, in a sense by introducing distributed computing over the net.
Lest the idea of horrible viruses loom in your mind, consider that applets run
under a layered, well-defined environment with user-controlled security
restrictions. Applets could be authenticated with public-key technology and encrypted
in transit (this is not implemented yet).
The built-in Java classes are very net-savvy, and provide for firewalls,
proxies, and default access control (for accessing the originating server only).
Depending on security, applets can seamlessly access other program components and
data, such as images and databases, across the Internet. These features are why Java
has been dubbed the Internet programming language.
As of this writing, Sun is working toward implementing the Java Virtual Machine
under the MacOS, and is also porting its HotJava browser to the Mac. Netscape is also
implementing a plug-in that allows Java applets to run in Navigator 2.0. On the
development side, Metrowerks and Symantec both announced Java support by adding a
new compiler to their suite of compiler plug-ins [and Borland recently made a similar
announcement - jaw]. Natural Intelligence of Cambridge, MA, was first to bring Java
to the Mac, by delivering their Java IDE, Roaster, at MacWorld San Francisco in
January; they implemented their Java environment working entirely from the
published Java specification. All compiler vendors are pledged to support applet and
stand-alone application development, integrated debugging, and a standard Java Virtual
Machine environment.
Another related component, JavaScript, became necessary to bridge the technical boundary between an HTML author with scripting skills and a full-fledged
programmer. The Java language is too unwieldy for a scripting-level webmaster,
partially because it also requires an external development environment. JavaScript is
the result of Sun and Netscape collaborating on an object-based end-user scripting
language capable of generating Java applets transparently. To minimize the need for
learning several scripting languages on several platforms, and to leverage the name
recognition of Java, Sun and Netscape agreed on enhancing Navigator 2.0’s
LiveScript. JavaScript and the Java language share no syntax or similarities.
JavaScript will most likely appeal to AppleScript-savvy users as a way to script
Netscape.
History of Java
Originally named Oak at Sun Microsystems, Java was intended to be the simple,
object-oriented, distributed language for the much-hyped $500 Internet set-top
boxes. At first, C++ was the language of choice for these consumer devices; but Sun
decided that constant recompilation of code, version control, and problems with
pointers, memory leaks, and security, to name a few, were defeating the development
effort. James Gostling, a principal architect of Java, decided he needed a new small,
reliable, portable, distributed, interactive language - one that would facilitate
distribution of updates, would attend to network security, and would not be as fragile
about changes in source code of classes.
The fragility of compiled code, even with shared libraries, was a primary
influence in Oak’s design. Oak needed the functionality of binary-compatible libraries
similar to IBM’s System Object Model (SOM) in a small footprint. Through its
simplicity (and real-world shipping schedule!), Oak fulfilled its intended purpose for
coding small, distributed, robust, platform-independent agents.
Unfortunately, the market and network infrastructure for Sun’s appliances did
not materialize. In late 1994, Oak was renamed and redirected towards the Internet. A
large part of the success of Java has been Sun’s brilliant positioning for the Web.
After brainstorming for the name “Java”, Sun sold the idea of applets to Netscape, and
devised the plan to write the HotJava browser to prove the language.
Amidst significant press coverage and extravagant predictions, Sun’s strategy had
worked. All the talk was of how to develop desktop applications that always load the
latest code from a server, and how applications could even be used on a metered basis
vs. purchased and licensed, while Java itself was still in beta. Even Microsoft, aware
of the momentum of Java, and at least a few months behind in defining a similar
language, redirected Blackbird, its Visual Basic/OLE-centric paradigm, and embraced
Java in its Internet strategy. (Technically, Blackbird lacked the cross-platform
momentum of Java because of the dependency on OLE; Blackbird was renamed to
Internet Studio and now focuses on implementing custom publishing needs with OLE
objects and Visual Basic support.)
Some Mac enthusiasts feared that Java was in direct competition with OpenDoc
and Cyberdog. In reality, though, Java represents another type of OpenDoc data that
Cyberdog interfaces with over the Internet. Cyberdog [see MacTech 12.2 (February,
1996) 35-39 - man] is a collection of OpenDoc parts Apple is designing to show how
document-centric computing works with the Internet. Apple or a third party will
simply implement an OpenDoc part that knows how to interpret Java bytecodes, and
presto, Java will be accessible with OpenDoc. Parts for OpenDoc can be written in a
variety of programming languages because there is a language-neutral binary
interface for linking and binding with a part’s interface. Since Java is a programming
language, it’s likely that a proper interface for developing OpenDoc parts in Java will
be defined; Java allows for external routines to be implemented on a specific platform,
and this would be the facility necessary to interface with OpenDoc.
Language Overview
The Java language was still in beta specification at the time of this writing. Java is
undergoing peer review on the Internet with enhancements made to usability and
security. It is unlikely that any major changes to the language will occur in the
interval before this article appears, though. Most of the enhancements and changes are
in the large number of built-in classes the Java runtime environment comes with.
Java as a language is difficult to describe because it resembles so many languages
including SmallTalk, Objective C, C++ and Eiffel. Fortunately, the similarities go a
long way toward learning Java for most programmers. Java was developed in the same
time frame as Apple Dylan, and represents a trend of reversal against the size and
complexity of C++. In this regard, Java is a simpler, strongly typed, strongly
object-oriented, memory management-safe, exception- and thread-aware language.
I’m going to start by using C++ as a reference and describing what is not in Java.
From there I will list some features Java adds to help you program more efficiently.
Of course, nothing replaces actually coding in the language itself, but this is a good
start.
Java, as opposed to C++, has:
• no preprocessor and no typedefs
• no stand-alone procedures and functions
• no structures or unions
• no unsigned numeric types
• no automatic type coercions, e.g. long to short (with data loss)
• no multiple inheritance and operator overloading
• no goto statement (goto is reserved)
• no pointers
• no multiple expression comma operator
This may sound a little limited and constrained, but let me describe what Java
does offer. Because Java doesn’t allow #define or typedef, when you look at a data
member, you don’t have to guess what it is; it’s either a built-in type or a class.
Without structs and unions, you must organize your data structures by using classes.
By disallowing stand-alone functions, Java forces you to organize your access to your
data and provide a public interface to class members. This means you must play by
true object-oriented rules.
Another nice feature to help you (my favorite) is labels on continue and break
statements. When you’re deep in several embedded loops, you no longer have to set
flags to let you know when to break out of each one; you can break out to the exact level
you want. Many routines have program states, graphics states or other conditions that
must be restored before exiting. If you use try/catch blocks you will see code