WebReference.com - Part 4 of chapter 5 from Beginning Java 2 SDK 1.4 Edition, Wrox Press Ltd (4/8)
[previous] [next] |
Beginning Java 2 SDK 1.4 Edition
Standard Packages
All of the standard classes that are provided with Java are stored in standard packages. There is a substantial (more than 130 in SDK1.4) and growing list of standard packages but some of the ones you may hear about quite frequently are:
java.lang | Contains classes that are fundamental to Java (e.g. the Math class) and all of these are available in your programs automatically. You do not need an import statement to include them. |
java.io | Contains classes supporting stream input/output operations. |
java.nio | Contains classes supporting new input/output operations in JDK1.4--especially with files. |
java.nio.channels | Contains more classes supporting new input/output operations in JDK1.4--the ones that actually read and write files. |
java.awt | Contains classes that support Java's Graphical User Interface (GUI). While you can use these classes for GUI programming, it is almost always easier and better to use the alternative Swing classes. |
javax.swing | Provides classes supporting the 'Swing' GUI components. These are not only more flexible and easier to use than the java.awt equivalents, but they are also implemented largely in Java with minimal dependency on native code. |
javax.swing.border | Classes to support generating borders around Swing components. |
javax.swing.event | Classes supporting event handling for Swing components. |
java.awt.event | Contains classes that support event handling. |
java.awt.geom | Contains classes for drawing and operating with 2D geometric entities. |
java.awt.image | Contains classes to support image processing. |
java.applet | This contains classes that enable you to write applets--programs that are embedded in a web page. |
java.util | This contains classes that support a range of standard operations for managing collections of data, accessing date and time information, and analyzing strings. |
The standard packages and the classes they contain cover an enormous amount of ground, so even in a book of this size it is impossible to cover them all exhaustively. There are now many more classes in the standard packages with JDK1.4 than there are pages in this book. However, we will be applying some classes from all of the packages in the table above, plus one or two others besides, in later chapters of the book.
Standard Classes Encapsulating the Basic Data Types
You saw in the previous chapter that we have classes available that allow you to define objects that encapsulate each of the basic data types in Java. These classes are:
Boolean | Character | Byte |
Short | Integer | Long |
Float | Double |
These are all contained in the package java.lang
along with quite a few other classes
such as the String
and StringBuffer
classes that we saw in Chapter 4, and the
Math
class. Each of these classes encapsulates the corresponding basic type, and includes
methods for manipulating and interrogating objects of the class, as well as a number of static methods
that provide utility functions for the underlying basic types. Each of the classes corresponding to a
numeric type provides a static toString()
method to convert to a String
object,
as we saw in the last chapter. There is also a non-static toString()
method in all of these
classes that returns a String
representation of a class object.
The classes encapsulating the numeric basic types each contain the static final
constants MAX_VALUE
and MIN_VALUE
that define the maximum and minimum values that
can be represented. The floating-point classes also define the constants POSITIVE_INFINITY
,
NEGATIVE_INFINITY
, and NaN
(stands for Not a Number as it
is the result of 0/0), so you can use these in comparisons. Alternatively, you can test floating point
values with the static methods isInfinite()
and isNaN()
--you pass your variable
as an argument, and the methods return true for an infinite value or the NaN
value
respectively. Remember that an infinite value can arise without necessarily dividing by zero. Any
computation that results in an exponent that is too large to be represented will produce either
POSITIVE_INFINITY
or NEGATIVE_INFINITY
.
Conversely there are methods to convert from a String
to a basic type. For example, the
static parseInt()
member of the class Integer
accepts a String
representation of an integer as an argument, and returns the equivalent value as type int
.
An alternative version of this method accepts a second argument of type int
that specifies
the radix to be used. If the String
object cannot be parsed for any reason, if it contains
invalid characters for instance, the method will throw an exception of type NumberFormatException
.
All the standard classes define methods to parse strings--parseShort()
, parseByte()
,
and parseLong()
.
There are many other operations supported by these classes so it is well worth browsing the JDK documentation for them.
[previous] [next] |
Created: July 29, 2002
Revised: July 29, 2002
URL: https://webreference.com/programming/java/beginning/chap5/4/4.html