Skip to main content
Engineering LibreTexts

5.4: From the Java Library java.lang.Math

  • Page ID
    15087
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    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

    lll
    Method&Description&Examples

    int abs(int x)& Absolute value of x& if x \(>\)= 0 abs(x) is x&& if x \(<\) 0 abs(x) is \(-\)x& & &&& Rounds x to the smallest& ceil(8.3) is 9 &integer not less than x& ceil(\(-\)8.3) is \(-\)8 &&& Rounds x to the largest& floor (8.9) is 8 &integer not greater than x& floor(\(-\)8.9) is \(-\)9 &&& Natural logarithm of x& log (2.718282) is 1.0 &&& x raised to the y power (\(x^y\))&pow(3, 4 ) is 81.0 &&pow(16.0, 0.5) is 4.0 &&& Generates a random& random() is 0.5551 &number in the interval [0,1)& random() is 0.8712 &&& Rounds x to an integer& round(26.51) is 27 &&round (26.499) is 26 &&& Square root of x& sqrt(4.0) is 2.0

    All Math methods are static class methods and are, therefore, invoked through the class name. For example, we would calculate \(2^4\) as , 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 it 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 for it here is that it provides a convenient and efficient way to introduce helpful math functions into the Java language.

    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 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.


    This page titled 5.4: From the Java Library java.lang.Math is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by Ralph Morelli & Ralph Wade via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.