Java Reference
In-Depth Information
a debugging aid and not as the final environment to allow users to run applets.
Nonetheless, applets are now often run as stand-alone programs using an applet
viewer.
1
We find this to be a somewhat unfortunate accident of history. Java has multi-
ple libraries of software for designing windowing interfaces that run with no connec-
tion to a browser. We prefer to use these libraries, rather than applets, to write
windowing programs that will not be run from a Web browser. In this topic we show
you how to do windowing interfaces as applets and as programs with no connection to
a Web browser. In fact, the two approaches have a large overlap of both techniques and
the Java libraries that they use. Once you know how to design and write either applets
or applications, it is easy to learn to write the other of these two kinds of programs.
An applet always has a windowing interface. An application program may have a
windowing interface or use simple console I/O. So as not to detract from the code
being studied, most of our example programs, particularly early in the topic, use sim-
ple console I/O (that is, simple text I/O).
A Sample Java Application Program
Display 1.1 contains a simple Java program and the screen displays produced when it
is run. A Java program is really a class definition (whatever that is) with a method
named
. When the program is run, the method named
is invoked; that is,
main
main
the action specified by
is carried out. The body of the method
is enclosed
main
main
in braces,
, so that when the program is run, the statements in the braces are exe-
cuted. (If you are not even vaguely familiar with the words
{}
class
and
method,
they will
be explained. Read on.)
The following line says that this program is a class called
:
FirstProgram
public class FirstProgram
{
The next two lines, shown below, begin the definition of the
method:
main
public static void main(String[] args)
{
,
and so forth mean will be explained in the next few chapters. Until then, you can
think of these opening lines, repeated below, as being a rather wordy way of saying
“Begin the program named
The details of exactly what a Java class is and what words like
,
,
public
static
void
.”
FirstProgram
public class FirstProgram
{
public static void main(String[] args)
{
1
An applet viewer does indeed use a browser to run an applet, but the look and feel is that of a stand-
alone program with no interaction with a browser.
Search WWH ::




Custom Search