48
CHAPTER 4. SUBROUTINES
143
The Java programming language is supplemented by a large, standard API. You’ve seen
part of this API already, in the form of mathematical subroutines such as Math.sqrt(), the
String data type and its associated routines, and the System.out.print() routines. The
standard Java API includes routines for working with graphical user interfaces, for network
communication, for reading and writing files, and more. It’s tempting to think of these routines
as being built into the Java language, but they are technically subroutines that have been
written and made available for use in Java programs.
Java is platform-independent. That is, the same program can run on platforms as diverse as
Mac OS, Windows, Linux, and others. The same Java API must work on all these platforms.
But notice that it is the interface that is platform-independent; the implementation varies
from one platform to another. A Javasystemon aparticular computer includes implementations
of all the standard API routines. A Java program includes only calls to those routines. When
the Java interpreter executes a program and encounters a call to one of the standard routines,
it will pull up and execute the implementation of that routine which is appropriate for the
particular platform on which it is running. This is a very powerful idea. It means that you only
need to learn one API to program for a wide variety of platforms.
4.5.2 Java’s Standard Packages
Like all subroutines in Java, the routines in the standard API are grouped into classes. To
provide larger-scale organization, classes in Java can be grouped into packages, which were
introduced briefly in Subsection 2.6.4. You can have even higher levels of grouping, since
packages can also contain other packages. In fact, the entire standard Java API is implemented
in several packages. One of these, which is named “java”, contains several non-GUI packages
as well as the original AWT graphics user interface classes. Another package, “javax”, was
added in Java version 1.2 and contains the classes used by the Swing graphical user interface
and other additions to the API.
Apackage can contain both classes and other packages. A package that is contained in
another package is sometimes called a “sub-package.” Both the java package and the javax
package contain sub-packages. One of the sub-packages of java, for example, is called “awt”.
Since awt is contained within java, its full name is actually java.awt. This package contains
classes that represent GUI components such as buttons and menus in the AWT. AWT is the
older of the two Java GUI toolboxes and is no longer widely used. However, java.awt also
contains a number of classes that form the foundation for all GUI programming, such as the
Graphics class which provides routines for drawing on the screen, the Color class which repre-
sents colors, and the Font class which represents the fonts that are used to display characters
on the screen. Since these classes are contained in the package java.awt, their full names
are actually java.awt.Graphics, java.awt.Color, and java.awt.Font. (I hope that by now
you’ve gotten the hang of how this naming thing works in Java.) Similarly, javax contains a
sub-package named javax.swing, which includes such GUI classes as javax.swing.JButton,
javax.swing.JMenu, and javax.swing.JFrame. The GUI classes in javax.swing, together
with the foundational classes in java.awt, are all part of the API that makes it possible to
program graphical user interfaces in Java.
The java package includes several other sub-packages, such as java.io, which provides fa-
cilities for input/output, java.net, which deals with network communication, and java.util,
which provides a variety of “utility” classes. The most basic package is called java.lang. This
package contains fundamental classes such as String, Math, Integer, and Double.
It might be helpful to look at a graphical representation of the levels of nesting in the