# Exploring the Basic APIs Part

1. Math declares double constants E and PI that represent, respectively, the natural logarithm base value (2.71828...) and the ratio of a circle's circumference to its diameter (3.14159...). E is initialized to 2.718281828459045 and PI is initialized to 3.141592653589793.

2. Math.abs(Integer.MIN_VALUE) equals Integer.MIN_VALUE because there does not exist a positive 32-bit integer equivalent of MIN_VALUE. (Integer.MIN_VALUE equals -2147483648 and Integer.MAX_VALUE equals 2147483647.)

3. Math's random() method returns a pseudorandom number between 0.0 (inclusive) and 1.0 (exclusive).

4. The five special values that can arise during floating-point calculations are +infinity, -infinity, NaN, +0.0, and -0.0.

5. Math and StrictMath differ in the following ways:

■ StrictMath's methods return exactly the same results on all platforms. In contrast, some of Math's methods might return values that vary ever so slightly from platform to platform.

■ Because StrictMath cannot utilize platform-specific features such as an extended-precision math coprocessor, an implementation of StrictMath might be less efficient than an implementation of Math.

6. The purpose of strictfp is to restrict floating-point calculations to ensure portability. This reserved word accomplishes portability in the context of intermediate floating-point representations and overflows/underflows (generating a value too large or small to fit a representation). Furthermore, it can be applied at the method level or at the class level.

7. BigDecimal is an immutable class that represents a signed decimal number (such as 23.653) of arbitrary precision (number of digits) with an associated scale (an integer that specifies the number of digits after the decimal point). You might use this class to accurately store floating-point values that represent monetary values, and properly round the result of each monetary calculation.

8. The RoundingMode constant that describes the form of rounding commonly taught at school is HALF_UP.

9. Biglnteger is an immutable class that represents a signed integer of arbitrary precision. It stores its value in two's complement format (all bits are flipped—Is to 0s and 0s to 1s—and 1 has been added to the result to be compatible with the two's complement format used by Java's byte integer, short integer, integer, and long integer types).

10. The purpose of Package's isSealed() method is to indicate whether or not a package is sealed (all classes that are part of the package are archived in the same JAR file). This method returns true when the package is sealed.

11. The answer is true: getPackage() requires at least one classfile to be loaded from the package before it returns a Package object describing that package.

12. The two main uses of the primitive wrapper classes are to store objects containing primitive values in the collections framework's lists, sets, and maps; and to provide a good place to associate useful constants (such as MAX_VALUE and MIN_VALUE) and class methods (such as Integer's parseInt() methods and Character's isDigit(), isLetter(), and toUpperCase() methods) with the primitive types.

13. You should avoid coding expressions such as ch >= '0' && ch <= '9' (test ch to see if it contains a digit) or ch >= 'A' && ch <= 'Z' (test ch to see if it contains an uppercase letter) because it is too easy to introduce a bug into the expressions, the expressions are not very descriptive of what they are testing, and the expressions are biased toward Latin digits (0-9) and letters (A-Z, a-z).

14. The four kinds of reachability are strongly reachable, softly reachable, weakly reachable, and phantom reachable.

15. A referent is the object whose reference is stored in a SoftReference, WeakReference, or PhantomReference object.

16. The References API's PhantomReference class is the equivalent of Object's finalize() method. Both entities are used to perform object cleanup tasks.

17. Listing 29 presents the Circle application that was called for in Chapter 6. Listing 29. Using asterisks to display a circle shape public class Circle {

final static int NROWS = 22; final static int NCOLS = 22; final static double RADIUS = 10.0;

public static void main(String[] args) {

// Create the screen array for storing the cardioid image. char[][] screen = new char[NROWS][]; for (int row = 0; row < NROWS; row++) screen[row] = new char[NCOLS];

// Initialize the screen array to space characters. for (int col = 0; col < NCOLS; col++)

screen[col] = ' '; for (int row = 1; row < NROWS; row++)

System.arraycopy(screen, 0, screen[row], 0, NCOLS);

// Create the circle shape.

// Output the screen array. for (int row = 0; row < NROWS; row++) System.out.println(screen[row]);

18. Listing 30 presents the PrimeNumberTest application that was called for in Chapter 6. Listing 30. Checking a positive integer argument to discover if it is prime public class PrimeNumberTest {

public static void main(String[] args) {

System.err.println("usage: java PrimeNumberTest integer"); System.err.println("integer must be 2 or higher"); return;

int n = Integer.parseInt(args);

System.err.println(n + " is invalid because it is less than 2"); return;

int n = Integer.parseInt(args);

System.err.println(n + " is invalid because it is less than 2"); return;

System.out.println (n + " is not prime"); return;

catch (NumberFormatException nfe) {

System.err.println("unable to parse " + args + " into an int");