25-06-2012, 11:48 AM
How To Write A Java Program
How to Write a Java Program.pdf (Size: 95.43 KB / Downloads: 26)
Developed by Sun, Java is succeeding
against ActiveX for
three reasons. First, it is a good
programming language - for all its
strengths, C++ is difficult, error-prone
and over-complicated. Visual Basic is
Windows-specific and only partially
object-oriented. Borland’s Delphi is
better but still Windows-specific.
Other languages such as SmallTalk or
Eiffel have advantages but have remained
a minority taste. Java is more
thoroughly object-oriented than C++,
safer to use because of automaticmemory
management, and easier to learn
and use because its designers avoided
many of the pitfalls in C++.
The second factor driving Java forward
is its portability. Java programs
run in an interpreter called the JVM
(Java Virtual Machine). In theory, this
means that a Java application can run
on any platform so long as a JVM is
installed. This is binary compatibility,
which means the same executable file
will run, as opposed to languages like
C, which can be portable at the source
code level but must be recompiled for
each platform.
If you are browsing the Web, this is
a huge advantage. It prevents the need
for Web servers to carry separate executable
files native to every possible
browsing platform. This is why Java
can be thought of as a platform in its
own right. It is also a benefit for companieswith
mixed-platform networks.
The JVMis also able to police the interaction
between the application and the
rest of the system, allowing Java to
have built-in security features.
The third reason for Java’s success
is that it’s now building on the critical
mass it has already achieved. Success
breeds success, and the availability of
libraries, components, development
tools, and people with the skills to use
them, is more important than the fundamental
qualities of a programming
language. Java’s adoption is only
partly thanks to its qualities as a language.
Interest in cross-platform solutions
had been waning because
Windows is used so widely on the
desktop. Companies including Sun,
IBM and Oracle are relentlessly promoting
Java in part to counter Microsoft’s
desktop stranglehold. In
consequence, developers can be confident
of Java’s future. Within a few
years, Java may be the leading generalpurposedevelopment
language, not just
for designing “intelligentWeb pages”.
Disadvantages
The main focus of this article is to
explain the fundamentals of writing
and deploying Java applications. You
should be aware, though, of Java’s disadvantages.
Because Java is interpreted,
programs run relatively
slowly. This is being addressed by improvements
to the JVM, just-in-time
compilers that create native executable
code on the fly, and full native-code
Issue 90 (December 1997) Page 13 File: T1122.1
No longer does a Web page need to consist purely of text and graphics. Java and ActiveX
are programming languages that turn Web pages into intelligent programs capable of
accepting input and performing processing. Using Java is not difficult and allows you to
extend the functionality of your Web site or intranet.
By Tim Anderson
How To Write
A Java Program
Figure 1 - The JDK is free, up-to-date, and resolutely non-graphical.
It is still the best way to learn Java.
PC Network Advisor
Tutorialrogramming
compilers.This last option comes at the
expense of cross-platform capability.
Another serious issue is compatibility.
Java is developing fast, and Sun is continually
releasing updates to the JDK
(Java Development Kit). These updates
require new versions of the JVM.
These JVMs come from numerous vendors,
and inevitably there is a time lag
between JDK updates and the availability
of compatible JVMs. For example,
at the time of writing Sun has
released the JDK 1.1.3, and will soon be
moving on to JDK 1.2. Most people run
Java through browsers, and released
versions of Netscape and Internet Explorer
do not support JDK 1.1. Even
when they do, there is still the problem
of oldversions that remain inuse. This
scenariowill be repeated until Java becomes
mature.
Another problem is that Microsoft
has stated that not all features of JDK
1.1will be implemented in Internet Explorer’s
JVM (which has, apparently,
irritated Sun to the point of threatening
to withdraw Microsoft’s licence to implement
Java at all). Finally, irrespective
of which version of JDK is supported,
each JVM is different. Java’s
cross-platform compatibility is not
perfect, so developers still need to test
and debug on each JVM.
Finally, you should be realistic about
the features available to you through
Java as opposed to native code. This
particularly applies to the graphicaluser
interface.Platformsarenotidentical,and
Javahastobecross-platform.Ifyouwant
to exploit all the features of, say, Windows,
Java is not for you and it’s probably
worth looking at ActiveX. Having
said that, Java’s interface widgets are
quite adequate for most purposes.
Applets And Apps
There are two kinds of Java application.
Applets are designed to run
within a Web browser, while applications
are standalone graphical or command-
line programs. Currently the
majority of Java programs are applets.
When applets are run fromthe Web or
across any network, they are subject to
security restrictions. Essentially, they
cannot communicate with any computer
other than the server fromwhich
they were downloaded. Thatmeans no
access to local files, and no power to
execute local external programs.
Compare this with ActiveX, where
it’s quite possible for an ActiveXapplet
(called a Control) to, say, delete files
from the user’s hard disk or infect it
with a virus without the user’s knowledge
or permission.
The JDK1.1 introduces the ability to
digitally sign applets stored in JAR (Java
Archive files, similar to ZIP files) format.
The idea is that youwill be able to
configure a browser to give additional
rights to signed applets from trusted
sources. You can already do this with
Internet Explorer, by packaging Java
applets in signed CAB (Cabinet - Microsoft’s
archive format) files. However
JAR is the official Java standard.
If you create Java applications, rather
than applets, the security restrictions do
not apply. You will still benefit from
the productivity of the Java language
and its cross-platform talents. The cost
is poor performance and limited interface
capabilities, when compared to a
native code executable. Java is still a
File: T1122.2 Issue 90 (December 1997) Page 14
class HelloJava {
public static void main-
(String args[])
{
System.out.println(“Hello Java”);
}
}
Figure 2 - A very simple Java
application.
import java.applet.Applet;
import java.awt.Graphics;
public class HelloApplet extends -
Applet {
public void paint(Graphics g)
{
g.drawString(“Hello Applet”,10,10);
}
}
Figure 3 - A simple Java applet.
<HTML>
<TITLE>Hello Applet</TITLE>
<BODY>
Here is the Hello applet:<p>
<APPLET CODE="HelloApplet.class" WIDTH=100 HEIGHT=100>
</APPLET>
</BODY>
</HTML>
Figure 4 - The host HTML file for the Java applet shown in Figure 3.
Figure 5 - The three parts of a Java applet: Java code, HTML,
and running the result in AppletViewer or a browser.
PC Network Advisor
Tutorialrogramming
good choice, but a less attractive option
than it is for applets.
Installing Java
To run Java applets, all you need is
a Java-capable browser such as
Netscape Navigator 2.0 or Microsoft
Internet Explorer 3.0. To do Java development,
you can either install one of
the Java development tools (see the
box) or install Sun’s JDK (Figure 1).
There are three good reasons to install
the JDK. First, it is a free download
from the Sun or JavaSoftWeb site. Second,
it is the definitive and most up-todate
Java. Third, it will give you a good
understanding of howJava works. The
JDK is a set of command-line tools including:
l Java core classes (stored in
classes.zip).
l Java interpreter (java.exe).
l Java compiler (javac.exe).
l AppletViewer (appletviewer.exe),
which lets you run applets outside
a browser for testing purposes.
l Other tools, source files and documentation.
The latest versions of the JDKinclude
a cut-down version of the Java interpreter,
jre.exe. This is intended for runtime
deployment of Java applications
(not just applets), on systemswhich do
not need the development tools.
Hello Java
Java is thoroughly object-oriented.
Everything is built from classes. The
simplest Java application looks something
like the code shown in Figure 2.
To compile and run this application
you would take the following steps:
1 Save it in a file called Hello-
Java.java. The name of the file must
match the name of the class. Java is
case-sensitive too.
2 Compile it from the command
prompt by typing javac Hello-
Java.java. All going well, this
creates a file called HelloJava.class,
which is a Java bytecode executable.
Bytecode is the binary format understood
by the Java virtualmachine.
3 Run it from the command prompt
with the command java Hello-
Java.
When you execute HelloJava, the
Java interpreter beginswith the main()
method of the HelloJava class. For this
to work, the system must be able to
find the Java interpreter, compiler, and
classes. Therefore, you would normally
add the jdk1.1.3\bin directory (for example)
to the path. Java also recognises
a CLASSPATH environment variable
pointing to the classes.zip file and
other classes needed by an application.
If you are having problems running
Java the PATH and CLASSPATH are
the first places to look.
Every class in Java has its own
.class file, although they may be archived
together in a single .ZIP or .JAR.
You can also organise classes into logical
groups called packages. If you designate
a class to be part of a package,
then you can refer to it using dot notation,
as in java.applet.Applet. Packages
are an essential organising tool. They
also help prevent duplicate class names.
Hello Java Applet
Leaving aside security considerations,
applets are very similar to applications.
Here are some key differences:
l Applets are kicked into life by a
Web browser. The browser positions
the applet based on the value
of certain HTML tags.
l Applets are always graphical applications.
l Applets have no main() method.
The browser instantiates the object,
and then calls amethod called init()
where you can put initialisation
code as required.
The simplest example is shown in
Figure 3. Save the applet as HelloApplet.
java and compile it as above. To
run the applet, you need to create a
host HTML file, such as the one shown
in Figure 4. Finally, to run the applet
you have two options. One is to use
AppletViewer (see Figure 5). At the
Figure 6 - Applets are able to
communicate with the browser,
as here where the button click
puts a message on the
browser’s status bar.
import java.applet.Applet;
import java.awt.*;
public class HelloApplet extends Applet
{
public void init()
{
add(new Button(“Click me”));
}
public boolean action(Event evt, Object arg)
{
if (arg.equals(“Click me”))
{
getAppletContext().showStatus(“You clicked!”);
return true;
}
return false;
}
}
Figure 7 - An applet that displays a message when a button is clicked.
Issue 90 (December 1997) Page 15 PC Network Advisor File: T1122.3
Writing Java
Tutorialrogramming
command prompt, enter Applet-
Viewer HelloApplet.html. Note
that it is the HTML file, not the class
file, which is the parameter for AppletViewer.
Alternatively, you can simply open
theHTML document in a Java-enabled
browser.