Skip to main content
Logo image

Java, Java, Java: Object-Oriented Problem Solving, 2024E

Section 5.6 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.6.1 lists some of the most commonly used Math class methods.
Table 5.6.1.
Method Description Examples
int abs(int x) Absolute value of x if x \(>\)= 0 abs(x) is x
long abs(long x) if x \(\lt\) 0 abs(x) is \(-\)x
float abs(float x)
int ceil(double x) Rounds x to the smallest ceil(8.3) is 9
integer not less than x ceil(\(-\)8.3) is \(-\)8
int floor(double x) Rounds x to the largest floor (8.9) is 8
integer not greater than x 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 (\(x^y\)) pow(3, 4 ) is 81.0
pow(16.0, 0.5) is 4.0
double random() Generates a random random() is 0.5551
number in the interval [0,1) 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

Subsection 5.6.1 Static Class Elements

The Math is somewhat exceptional in terms of what we’ve already learned about Java classes. Here is an outline of how it is defined:
public final class Math    // Final, can't subclass
{   private Math() {}      // Private, can't invoke
    ...
    public static native double sqrt(double a)
          throws ArithmeticException;
}
Notice that the class is declared public final. This means that it can be accessed (public) but it cannot be extended or subclassed (final). Notice that its default constructor is declared private. This prevents this class from being instantiated — i.e., there can be no Math objects.
The idea of a class that cannot be subclassed and cannot be instantiated may seem a little strange at first. The justification for it here is that it provides a convenient and efficient way to introduce helpful math functions into the Java language.
Notice also that the sqrt() method is declared static, as are all Math class methods:
public static native double sqrt(double a)
      throws ArithmeticException;
This means that when we want to use a Math method, we have to call it using the class name. For example, we would calculate \(2^4\) 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.
Defining the Math class in this way makes it easy to use its methods, because you don’t have to create Math objects. It is also a very efficient design because java.lang.Math methods are loaded into memory at the beginning of your program’s execution, and they persist in memory throughout your program’s lifetime.
Random numbers are very useful in coding, for example in games. Math.random() returns a random number between 0.0-0.99. We often need to move the random number to be in a certain range, for example to get a random number between 1 to 10. (int)(Math.random()* range) + minmoves the random number into a range starting from a minimum number. The range is the (max number - min number + 1).
// returns a random number between 0-.99
double randDec = Math.random();
// returns a random number between 1 and 10
int randNum = (int)(Math.random()* 10) + 1;

Activity 5.6.1.

Run the code below to see the Math methods being used. Run it several times to see how the random value changes each time. How could you change the code to return a random integer from 5 to 15?
In general, static class elements — methods and variables — are useful when we need elements that don’t depend on the existence of objects of a class. For example, consider the following class definition:
public Widget
{   
    public static nWidgets = 0;
    public Widget() 
    {
      nWidgets++;
    } 
}
Here we have a public static variable, nWidgets that is meant to keep a count of the number of widgets created. The count is incremented every time the constructor is called — i.e., every time a widget is created. Obviously, before the first widget is created we want to be able to say that the count is 0. We can do so by accessing Widget.nWidgets.
We will see other occasions to use the static and final qualifiers in subsequent chapters.
You have attempted of activities on this page.