From: www.itworld.com
October 19, 2001 —
Java is the new language developed by Sun
Microsystems that significantly raises the level of technology on the
Web. Java allows individuals, Web designers, and organizations large
and small to build highly interactive and safe applications. The
technology is based on
Running some Java applets will demonstrate the above features. For
those of you with a mathematical or financial background run the derivative calculator applet
at http://zeitgeist.com. For those of you with a consumer preference,
there is the multimedia application at http://www.vectorman.com. (If you do not have HotJava and you click
on the link, your browser should just ignore the applet.)
For those of you using HotJava you have just downloaded a
program that can be run on your system. This same program runs on
Solaris for SPARC, Solaris for Intel, Windows 95, and NT without
modification.
HotJava Alpha 3 is the current release of the browser. Until now,
releases contained a browser and interpreter, with its corresponding
API. The JDK (Java Development Kit) is the next iteration of the API.
The JDK does not contain a browser but does contain an
appletviewer, which allows you to view applets for debugging
and development, and deployments that use no HTML other than a tag to
call an applet. More
information on obtaining the JDK is available.
Alpha 3 applets do not work with the JDK or Netscape. Small
modifications are required. The procedure for converting an Alpha 3
applet is described in Converting Alpha 3
applets (http://java.sun.com/JDK-prebeta1/converting/). The HTML interface language was changed slightly,
requiring some minor edits to applets. To get an Alpha 3
applet working with the JDK and Netscape you need to convert the applet
and modify the HTML file that calls the applet.
At the time of this writing a new Netscape version of a browser that
supports Java Applets was not released. To avoid confusion, we will
discuss Netscape's Java abilities in the next issue and focus on
HotJava this month.
Let the games begin
The HotJava browser reads standard HTML just like any other Web
browser. It also has the ability to download Java applets. To
accomplish this, a small addition was made to HTML. An additional tag
was added (<APPLET>) that allows the specification of a Java applet class to be
loaded. When a user clicks on an application that requires an applet, the HotJava browser calls a thread that loads the
applet from the network or local filesystem. The code that is loaded is
the architecture-neutral bytecodes produced by the Java compiler. These
bytecodes are verified and then decoded into machine instructions. It
is hoped future versions will compile these bytecodes to native machine
instructions for improved performance.
Many languages share some of the features of Java. The Java designers
built a practical language for distributed computation without the
complexity associated with such languages. Why another language? It
can be argued that there are systems with enough ubiquity (consider
the proliferation of DOS/Windows 3.x) that there is no need for
Java's benefits. Many years ago, Japan launched a project
called TRON. The purpose of this project was to provide one OS and one
hardware platform for the entire country of Japan. This project
ultimately failed. Java, unlike TRON, is based on standards, proven
network infrastructure, and is architecture independent at runtime.
The salient features of the Java Language are:
NULL pointer would notclass A {
public int amount;
private int total;
}
p = new amount
p = p + 4; // Find location of private member
*p = 10000;
In Java this is not possible. All accesses to memory areas must be
done using object instance variables. This feature provides additional
security protection against internal security threats -- programmers
leaving a Trojan horse that accesses private data. It also provides
protection against inadvertent pointer over-run or under-run errors, as
well as incorrect offset calculations.
A typical programming error is introduced by a casting operation. Consider
the structure foo defined below. This structure has a couple of
useful members -- a name field, and an id field:
struct foo {
char name[10];
int id;
}
If this structure was known to your program, you could access
any piece of memory as a structure of type foo by simply
using this fragment of code:
struct foo *a;
a = (struct foo *)(0x1234);
printf("user %s, id = %d\n",a->name,a->id);
However if the memory so addressed was not a structure of type foo
your results would be unpredictable at best. In Java this is not
possible. All accesses to memory areas must be done using object
references. When used, these references are checked for type
compliance. Loosely typed data structures defined by
struct or typedef in C or C++ are replaced
with a strongly typed data structure defined by class in Java.
record = (struct foo *)malloc(sizeof (struct foo)); ... some code record = next_record
In C and C++ we would be left with a memory block in the heap without
a corresponding pointer. In Java, once this allocated memory was no
longer referenced, it would automatically be returned to the heap. In
C and C++ you have to perform frees over and over again and keep track
of a pointer that is separate from the object in some cases.
Another advantage is the number of errors that are prevented when one
portion of the C code calls free() to return allocated
memory to the heap but another portion is still holding pointer to it. If
the memory gets re-used the program will crash when the stale pointer
is used, sometimes much later in the execution. Problems of this type
are hard to debug and often complicated by timing differences and code
paths. In Java, a section of memory is not reclaimed until there are
no more references to it in the active code.
synchronized thatpublic boolean synchronized enqueue(Item i){
... Implementation
}
public boolean synchronized enqueue(Item i){
... Implementation
}
Alternatively one can code a lock only around the section of code that
needs to be locked. This would allow other callers to execute more
code before blocking. For very small functions this overhead may not
be worth it:
public class SomeClass {
Lock myLock = new Lock();
void someMethod() {
myLock.lock();
try {
StartOperation();
ContinueOperation();
EndOperation();
}
finally {
myLock.unlock();
}
}
}
Security -- downloading code over the Internet can be safe
There are several layers to the Java security model. These layers
build on each other to provide built-in security that can be extended
and or modified.
HOTJAVA_READ_PATH -- used to determine where an applet:$HOME/public_html, whereHOTJAVA_WRITE_PATH -- used to determine where an applet hasNULL.A common technique for compromising a system is to replace a good
program with a doctored copy. Often this cannot be done directly. An
indirect approach looks to replace an applet or class in the search
path. HotJava always searches for built-in functions in explicit
areas. This design limits spoofing since built-ins are always checked
first
Late-breaking news
While this article was being written the beta API was released. This
API is referred to as the JDK -- Java development Kit. The JDK offers
many new improvements and new functionality. Two of our favorite
additions are the Java Debugger and the Java Applet Viewer. The imaging
model has also been changed significantly and offers an excellent
graphics foundation that understands various color models as well as
the performance issues with loading images over slow lines. We'll
discuss these and other parts of the JDK in future columns. In the
mean time, additional information on several topics is available
below:
Uses for Java
Java will play a key role in defining the infrastructure of the
emerging electronic world. We can actually think of several hundred
uses for Java. Here are some of the more common:
Future columns
In future columns we will discuss:
Next month, we discuss Netscape 2.0, including example applets
demonstrating the use of various methods.
Unix Insider