Math Class

Package: java.lang

Provides built-in methods that perform a wide variety of mathematical calculations, including

check.png Basic functions, such as calculating an absolute value or a square root

check.png Trigonometry functions, such as sin and cos

check.png Practical functions, such as rounding numbers or generating random numbers


The Math class provides to static fields that define frequently used mathematical constants.


What It Is


static double E

The base of natural logarithms


static double PI

The constant pi (π), the ratio of a circle’s radius and diameter


Note that these constants are only approximate values because both π and e are irrational numbers.

Here’s an example of a statement that calculates the circumference of a circle:

double diameter = 4;

double circumference = Math.PI * diameter;


All the methods of the Math class are declared as static methods.



abs( argument)

Returns the absolute value of the argument. The argument can be an int, a long, a float, or a double. The return value is the same type as the argument.

cbrt( argument)

Returns the cube root of ...

Get Java For Dummies Quick Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.