Java Reference
In-Depth Information
finite() and isNaN() — you pass your variable as an argument, and the methods return true for an in-
finite value or the NaN value, respectively. There are also non-static versions for use with an object of the
class type. Remember that an infinite value can arise without necessarily dividing by zero. Any computa-
tion that results in an exponent that is too large to be represented produces either POSITIVE_INFINITY or
NEGATIVE_INFINITY .
Many other operations are supported by these classes, so it is well worth browsing the JDK document-
ation for them. In particular, the Character class defines a large number of static methods for testing and
classifying characters.
Autoboxing Values of Primitive Types
Circumstances can arise surprisingly often where you want to pass values of a primitive type to a method
that requires the argument to be a reference to an object. The compiler supplies automatic conversions of
primitive values to the corresponding class type when circumstances permit this. This can arise when you
pass a value of type int to a method where the parameter type is type Integer , for example. Conversions
from a primitive type to the corresponding class type are called boxing conversions , and automatic conver-
sions of this kind are described as autoboxing .
The compiler also inserts unboxing conversions when necessary to convert a reference to an object of a
wrapper class for a primitive type such as double to the value that it encapsulates. The compiler does this
by inserting a call to the xxxValue() method for the object. You can see this in action in the following little
example.
TRY IT OUT: Autoboxing in Action
This program is contrived to force boxing and unboxing conversions to occur:
public class AutoboxingInAction {
public static void main(String[] args) {
int[] values = { 3, 97, 55, 22, 12345 };
// Array to store Integer objects
Integer[] objs = new Integer[values.length];
// Call method to cause boxing conversions
for(int i = 0 ; i < values.length ; ++i) {
objs[i] = boxInteger(values[i]);
}
// Use method to cause unboxing conversions
for(Integer intObject : objs) {
unboxInteger(intObject);
}
}
Search WWH ::




Custom Search