Java Reference
In-Depth Information
as 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 multiple
libraries of software for designing windowing interfaces that run without a connection
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 this topic, use
simple 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
main . When the program is run, the method named main is invoked; that is, the action
specified by main is carried out. The body of the method main is enclosed in braces,
{} , so that when the program is run, the statements in the braces are executed. (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 main method:
public static void main(String[] args)
{
The details of exactly what a Java class is and what words such as public , static ,
void , and so forth mean will be explained in the next few chapters. Until then, think
of these opening lines, repeated below, as being a rather wordy way of saying “Begin
the program named 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