Learn Java

Java Math

To efficiently perform various basic (and not so basic) numeric operations, including exponential, modulus or finding the maximum or minimum of two numbers, Java provides the standard class Math. Let's consider some of the most common methods of this class.

Basic and rounding methods

There are a number of popular methods for rounding numbers up and down and performing other basic operations. Let's take a look at some of them:

  • Math.min(..., ...) — returns the smaller value of two arguments;
  • Math.max(..., ...) — returns the greater value of two arguments;
int min = Math.min(11, 81); // min is 11
int max = Math.max(20, 30); // max is 30
  • Math.abs(...) — returns the absolute value of its argument;
int abs = Math.abs(-10); // abs is 10
double dabs = Math.abs(-10.33); // dabs is 10.33
  • Math.floor(...) — returns the largest double value that is less than or equal to its argument and is equal to a mathematical integer;
  • Math.ceil(...) — returns the smallest double value that is greater than or equal to its argument and is equal to a mathematical  integer.
double floor = Math.floor(3.78); // floor is 3.0
double ceil = Math.ceil(4.15); // ceil is 5.0

In simple terms, floor() method rounds down the double to its nearest integer, whereas ceil() rounds up the double to its nearest integer.

Exponential functions

When we need to calculate a square or a cube root of a given number, we can apply the following methods:

  • Math.sqrt(...) — returns the square root of its argument;
  • Math.cbrt(...) — returns the cube root of its argument;
double sqrt = Math.sqrt(2); // sqrt is 1.4142...
double cbrt = Math.cbrt(27.0); // cbrt is 3.0

It is also possible to raise a number to any power we would like:

  • Math.pow(..., ...) — returns the value of the first argument raised to the power of the second argument.
double square = Math.pow(5, 2); // the square of 5 is 25.0
double cube = Math.pow(2, 3); // the cube of 2 is 8.0

Trigonometric functions

And here are some of the trigonometric functions provided by Math:

  • Math.sin(...) — returns the trigonometric sine of the given angle in radians;
  • Math.cos(...) — returns the trigonometric cosine of the given angle in radians;
double sin = Math.sin(pi / 2); // sin90°  is 1.0
double cos = Math.cos(pi); // cos180° is -1.0
  • Math.toRadians(...) — converts an angle measured in degrees to an angle measured in radians (approximately).
double rad = Math.toRadians(30); // rad is 0.5235...

And there's more...

There are also methods for hyperbolic, logarithmic, angular, and other functions. Check them out here and use when needed. Among them, you'll find another useful method:

  • Math.random() — returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.
double random = Math.random(); // a random value >= 0.0 and < 1.0

Apart from basic math functions, the Java Math class contains two common constants:

  • Math.PI — is the ratio of the circumference of a circle to its diameter;
  • Math.E — is the base of the natural logarithm.
double pi = Math.PI; // pi is 3.1415...
double e = Math.E; // e is 2.71828...

The length of the hypotenuse

Now let's take a look at an example. Assume that we have a right-angled triangle (only one of three angles is 90 degrees). We know the lengths of two sides that form the 90 degree angle: a = 3 and b = 4. Our task is to calculate the length of the hypotenuse. Now, it is time to go through the list of the Math class functions. After finding the one we need, the only thing left is to write the following code:

double a = 3, b = 4;
double c = Math.hypot(a, b); // c is 5.0

As you can see, the Math class offers a wide range of methods for performing numeric calculations and using them will make your life easier and your code nicer.

Create a free account to access the full topic

“It has all the necessary theory, lots of practice, and projects of different levels. I haven't skipped any of the 3000+ coding exercises.”
Andrei Maftei
Hyperskill Graduate