Section 5.4. From the Java Library: java.lang.Math


[Page 215]

5.4. From the Java Library: java.lang.Math

The java.lang.Math class provides many common mathematical functions that will prove useful in performing numerical computations. As an element of the java.lang package, it is included implicitly in all Java programs. Table 5.11 lists some of the most commonly used Math class methods.

Table 5.11. A selection of Math class methods

Method

Description

Examples

int abs(int x)

Absolute value of x

if x >= 0 abs(x) is x

long abs(long x)

 

if x < 0 abs(x) is x

float abs(float x)

  

int ceil(double x)

Rounds x to the smallest integer not less than x

ceil(8.3) is 9
ceil(8.3) is 8

int floor(double x)

Rounds x to the largest integer not greater than x

floor (8.9) is 8
floor(8.9) is -9

double log(double x)

Natural logarithm of x

log (2.718282) is 1.0

double pow(double x, double y)

x raised to the y power (xy)

pow(3, 4 ) is 81.0
pow(16.0, 0.5) is 4.0

double random()

Generates a random number in the interval [0,1)

random() is 0.5551
random() is 0.8712

long round(double x)

Rounds x to an integer

round(26.51) is 27
round (26.499) is 26

double sqrt(double x)

Square root of x

sqrt(4.0) is 2.0


java.sun.com/docs


All Math methods are static class methods and are, therefore, invoked through the class name. For example, we would calculate 24 as Math.pow(2,4), which evaluates to 16. Similarly, we compute the square root of 225.0 as Math.sqrt(225.0), which evaluates to 15.0.

Indeed, Java's Math class cannot be instantiated and cannot be subclassed. Its basic definition is

public final class Math    // Final, can't subclass {   private Math() {}      // Private, can't invoke     ...     public static native double sqrt(double a)            throws ArithmeticException; } 


By declaring the Math class public final, we indicate that it can be accessed (public) but cannot be extended or subclassed (final). By declaring its default constructor to be private, we prevent this class from being instantiated. The idea of a class that cannot be subclassed and cannot be instantiated may seem a little strange at first. The justification here is that it provides a convenient and efficient way to introduce helpful math functions into the Java language.


[Page 216]

Defining the Math class in this way makes it easy to use its methods, because you don't have to create an instance of it. It is also a very efficient design because its methods are static elements of the java.lang package. This means that they are loaded into memory at the beginning of your program's execution, and they persist in memory throughout your program's lifetime. Because Math class methods do not have to be loaded into memory each time they are invoked, their execution time will improve dramatically.

Effective Design: Static Methods

A method should be declared static if it is intended to be used whether or not there is an instance of its class.





Java, Java, Java(c) Object-Orienting Problem Solving
Java, Java, Java, Object-Oriented Problem Solving (3rd Edition)
ISBN: 0131474340
EAN: 2147483647
Year: 2005
Pages: 275

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net