Java Reference
In-Depth Information
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 N ot a N umber 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.
Controlling Access to Class Members
We have not yet discussed in any detail how accessible class members are outside a class. You know
that from inside a static class method you can refer to any of the static members of the class, and a non-
static method can refer to any member of the class. The degree to which variables and methods within
one class are accessible from other classes is more complicated. It depends on what access attributes
you have specified for the members of a class, and whether the classes are in the same package. This is
why we had to understand packages first.
Search WWH ::




Custom Search