How Java Math class works

You can carry out simple calculations with Java Math. It has different methods that cover logarithms and trigonometry, as well as all of the basics. The syntax is comparatively simple, making it easy to learn.

What is Java Math?

If you want to perform basic numerical calculations in Java, you can use its Math class. The Java class doesn’t need to be imported separately and has numerous methods which we’ll cover in more detail later on in this article.

The Math class is not instantiated, and its methods are only accessed statically. The two constants of the class are also static: Euler’s number (approximately e = 2.7182818284590), which is the basis for the natural logarithm and the natural exponential function, and the number Pi (approximately π = 3.1415926535). The programming language’s Math class is contained in the java.lang package, and calculation results of this class are usually of the double data type.

Web Hosting
Secure, reliable hosting for your website
  • 99.9% uptime and super-fast loading
  • Advanced security features
  • Domain and email included

How to perform different calculations with Java Math

The best way to understand the functionality and syntax of the Java class Math is to use examples. It’s easier to understand the class and how it’s used in the context of individual methods. Below, we’ve included a range of different calculations to show you how the class works.

Determine absolute values

If you want to determine the absolute value of a parameter, you can use abs(). An absolute value is the distance between a number and 0 or an unsigned number. This means the result will always be positive. The data types permitted for this method are double, float, int and long. Below, we’ll show you how Math.abs works with a positive number. For the output in the examples that follow, we’ll use the Java command System.out.println.

public class Main {
public static void main(String args[]) {
int number = +7;
System.out.println ("The original number is: " + number);
System.out.println ("The absolute number is: " + "Math.abs (" + number + ") = " + Math.abs(number));
}
}
java

The output looks like this:

The original number is: 7 
The absolute number is: Math.abs ( 7 ) = 7
java

The initial value can also be negative. The result will still be positive. Let’s see what happens when we make the 7 in the above example negative:

public class Main {
public static void main(String args[]) {
int number = -7;
System.out.println ("The original number is: " + number);
System.out.println ("The absolute number is: " + "Math.abs (" + number + ") = " + Math.abs(number));
}
}
java

The output is largely the same as that of the previous example:

The original number is: -7 
The absolute number is: Math.abs( -7 ) = 7
java

The method ignores the sign of the negative integer (-7) and outputs 7 as the result.

Determine the largest value

Use the max() method to determine the larger value of two inputs. Here’s how it works:

public class Main {
public static void main(String args[]) {
double number = Math.max(3, 9);
System.out.println ("The larger number is: " + number);
}
}
java

The output is:

The larger number is: 9.0
java

Determine the smallest value

The code for determining a smaller value is similar to the code in the previous example. Use the method min() to do this:

public class Main {
public static void main(String args[]) {
double number = Math.min(3, 9);
System.out.println ("The smaller number is: " + number);
}
}
java

Here’s the output:

The smaller number is: 3.0
java

Calculate powers

While the previous examples were quite simple, there are more sophisticated calculations that the Java class Math can do. For example, you can calculate powers as well. The method for calculating powers is called pow(). With this method, we need to first define a base and an exponent before carrying out the calculation.

public class Main {
public static void main(String args[]) {
double base = 4;
double exponent = 2;
double power = Math.pow(base, exponent);
System.out.println ("The result is: " + power);
}
}
java

This is what the output will look like:

The result is: 16.0
java

Calculate square roots

The class can also be used for square root calculations with the sqrt() method. In the following example, we’ll calculate the square root of 64:

public class Main {
public static void main(String args[]) {
double number = 64;
double root = Math.sqrt(number);
System.out.println ("The result is: " + root);
}
}
java

This is the output:

The result is: 8.0
java

Generate random numbers

With the random() method, Java generates a random number between 0.0 and 1.0 or in a range that you specify yourself.

public class Main {
public static void main(String args[]) {
double randomNumber;
System.out.println(Math.random());
}
}
java

A possible output would be:

0.7488711506123137
java

However, you can also limit the possible results, for example, by only allowing whole numbers between 0 and 100. To do this, use the following code:

public class Main {
public static void main(String args[]) {
int randomNumber = (int) (Math.random() * 101);
System.out.println(randomNumber);
}
}
java

This will give you a random result like this:

27
java

What are the most important methods?

There are numerous methods that you can use with the Java Math class. We’ve listed the most important ones for you here:

Method Function
abs() Returns the absolute value of an argument
max() Returns the larger of two values
min() Returns the smaller of two values
pow() Returns the power value
sqrt() Calculates the square root
random() Returns a random double value
cbrt() Calculates the cube root
log() Returns the natural logarithm of a double value
sin() Calculates the sine of a double value
cos() Calculates the cosine of a double value
tan() Calculates the tangent value of a double value
round() Rounds a double value up or down to an integer
negateExact() Displays the opposite value of an argument
floor() Rounds down the largest double value that is less than or equal to the given argument
Was this article helpful?
Page top