Standard Mathematical Methods

In this lesson, we will learn to use the standard Math class to evaluate mathematical functions such as the square root and cosine.

The Class Math

Java provides a collection of methods that compute standard mathematical functions such as the square root, the cosine, and so on. The collection also includes two useful constants: PI—the value π—and E—the base e of the natural logarithms. This collection of methods and constants is organized as the class Math, much as the methods that read input data is organized as the class Scanner. We can invoke any method in the class Math by writing a statement in the following form:

variable = Math.method_name(. . .);

We also can embed Math.method_name(. . .) within an arithmetic expression.

The value or values that occur between the parentheses are called arguments. Thus, we pass arguments to a method when we call—or invoke—it, and it returns a result to us. Note that not all methods require arguments. Even so, the parentheses are always required, though there may be nothing inside them.

Here are some of the available methods in the class Math. The online documentation for the Java Class Library describes additional methods.

Get hands-on with 1400+ tech skills courses.