latest revision Oct 24 / 2000
references:
'Just Java', P.V.Linden, 'The
Java Certification Study Guide' Heller&Roberts, JDK 1.2.2 documentation
Wrapper Classes
Sometime you will need to treat a primitive type, ( i.e. int ), as an
object. For instance, when
adding a number to a Vector. Wrapper
classes encapsulate primitive types so they can be
treated like an object type. For instance, Integer
is the wrapper class for integers. A wrapper
class exists for each of the primitive types.
The Wrapper class name for each primitive can be derived by creating
the unabreviated version
of the primitive type name and capitalizing
it. For example, the int type's wrapper class is
the Integer class, while the char type's wrapper isthe
Character class. All the primitives and
their corresponding wrapper classes are listed in the following table.
Wrapper Class for each Primitive
Type
Wrapper | Primitive |
Boolean | boolean |
Character | char |
Byte | byte |
Short | short |
Integer | int |
Long | long |
Float | float |
Double | double |
Besides providing objects types for primitives, wrapper classes store
useful constants and
methods to convert to and from String
and other types.
More on Wrappers
Wrappers also make it possible to dointrospectionon primitive types. Introspection is important to the process of how java beans work as well as to the process of serialization. Introspection is
a java tool used to extract information about object types. It is
required for
Serialization
is the process of adding type information to a streamed object so that
the state
|
Example
/* The number converts to exponential
floating point form at 10 million. A NumberFormatException
is thrown when the range of an int is exceeded, ~ +/- 2/1 billion. */ class wrap{
|
The Math Class
Math contains a collection of math methods and
two constants. The constants are Math.PI
and Math.E.
Both are public, static, final and double. The math class
itself is final so cannot
be extended. It's constructor is private
so it cannot be instantiated. It's methods and constants
are static so can be accessed through
the class name.
A Brief look at Math class Methods.
abs( )
There are four versions of abs( ), each which
returns an absolute value for a value
provided which may be int, long, float, or double.
ceil( ) &
floor( )
These methods take and return doubles corresponding
to the nearest int value above
or below the double provided
max( ) &
min(
)
There are four versions of each max( ) and min(
) for int, long, float and double types.
These methods take two values and return the
greater or the lesser of two.
double random(
)
random( ) returns a random number between 0.0
and just less than 1.0. This method
acts as a wrapper for the creation of an instance
of the Random class which is one of
the utility methods supplied in the java.util
package.
// Random # formula:
random number = (int) ( Math.random( ) * number_range) + 1
round( )
Two forms of this method are available to round
a float (to an int) or a double (to a long)
sin( ), cos( ) &
tan( )
// there are also asin
acos atan & atan2
These methods take doubles and return the sine,
cosine, and tangent as doubles.
double sqrt( double
d )
sqrt( ) returns the square root of a double provided
as an argument
double pow(double
number, double power)
pow( ) returns a number evaluated to a given
power
log( )
Returns the natural logarithm (base e) of a double
value.
exp( )
Returns the exponential number e (i.e., 2.718...)
raised to the power of a double value.
rint( )
takes a double, rounds to an int then returns
this value as a double
IEEEremainder( )
returns an IEEE formulated remainder of a division
toDegrees( ) //
new with JDK1.2
Converts an angle measured in radians to the
equivalent angle measured in degrees.
toRadians( ) //
new with JDK1.2
Converts an angle measured in degrees to the
equivalent angle measured in radians.
Using Math methods
/* not catching number format exception (Try entering something like 5y5) */ class mathMethods{
for ( int i=0; i < intArray.length - 1; i++ ){
|