The Math Class

Code Bar
Math.abs(int); Absolute Value Function for Ints
Math.abs(double); Absolute Value Function for Doubles
Math.pow(double, double); Power Function
Math.sqrt(double); Square Root Function
Math.random(); Random Number Generator

As you may have noticed, the Java set of basic numeral operators is missing some fundamental functions, such as exponents and absolute values. Java wrapped all of those into a helper class named Math. Inside the Math class, Java has implemented the absolute value function, the power function, and the square root function. The Math class also holds Euler’s number and Pi as constants. And the final method you need to know in the Math class is the random method; it functions as a random number generator.

The syntax for the Math class’s methods:

First you type Math. Math is the class from which the method is being called. Next you place at period, and finally the method. The method should be followed by parenthesis and a semicolon. Inside the parenthesis should be the list of parameters needed.

For the math absolute value function you would type Math.abs();. Inside the parenthesis you would enter the parameters. The absolute function accepts either a double or an int. The function will return the absolute value of the parameter in same type entered.

    System.out.println(Math.abs(5));        // 5

    System.out.println(Math.abs(-5));       // 5

    System.out.println(Math.abs(5.0));      // 5.0

    System.out.println(Math.abs(-5.0));     // 5.0

The power function in the Math.class takes two doubles and returns the first double raised to the second double. Since ints are truncated doubles, inputting ints will work, but however the answer will still be returned in double form. For example, take a look at the following code, which prints the squares and cubes of the numbers 1 to 3:

    /* Squares */

    System.out.println(Math.pow(1, 2)); // 1^2 == 1.0

    System.out.println(Math.pow(2, 2)); // 2^2 == 4.0

    System.out.println(Math.pow(3, 2)); // 3^2 == 9.0

    /* Cubes */

    System.out.println(Math.pow(1, 3)); // 1^3 == 1.0

    System.out.println(Math.pow(2, 3)); // 2^3 == 8.0

    System.out.println(Math.pow(3, 3)); // 3^3 == 27.0

The square root function takes only one double, and returns the square root of it. Like the power function inputting ints will also work, but it will return a double answer. See the code below:

    System.out.println(Math.sqrt(1));       // 1.0

    System.out.println(Math.sqrt(4));       // 2.0

    System.out.println(Math.sqrt(9.0));     // 3.0

And the final function Math.random() produces a random double above or equal to 0, and below 1. If you multiple that returned digit by a number, then you can use it essentially as a random number generator. The following code will print a random number between one and hundred. First Math.random() returns a number between 0 and 1 (0, 1]. Then, after being multiplied by 100, the value is between 0 and 99.99… If we truncate this new value we get from 0 to 99. Finally, when you add one to the set, you get the range of 1 to 100.

    int x = (int) (Math.random() * 100) + 1;

The final thing to remember is that the Math class holds the constant Pi and Euler’s Number.

    double x = Math.PI; // 3.141592...

    double y = Math.E;  // 2.718281...

Lesson Quiz

1. What is the output of the following code blocks?

    int x = Math.abs(3) + Math.abs(-4);

a. 5
b. 6
c. 7
a. 4
b. 4.0
c. -4
    System.out.println(Math.pow(4.0, 3.0));
a. 7.0
b. 12.0
c. 64.0
    System.out.println(Math.sqrt(Math.pow(4.0, 4.0)));
a. 16
b. 24
c. 64

Written by Jason Zhang

Notice any mistakes? Please email us at [email protected] so that we can fix any inaccuracies.