You can carry out simple cal­cu­la­tions with Java Math. It has different methods that cover log­a­rithms and trigonom­e­try, as well as all of the basics. The syntax is com­par­a­tive­ly simple, making it easy to learn.

What is Java Math?

If you want to perform basic numerical cal­cu­la­tions in Java, you can use its Math class. The Java class doesn’t need to be imported sep­a­rate­ly and has numerous methods which we’ll cover in more detail later on in this article.

The Math class is not in­stan­ti­at­ed, and its methods are only accessed sta­t­i­cal­ly. The two constants of the class are also static: Euler’s number (ap­prox­i­mate­ly e = 2.7182818284590), which is the basis for the natural logarithm and the natural ex­po­nen­tial function, and the number Pi (ap­prox­i­mate­ly π = 3.1415926535). The pro­gram­ming language’s Math class is contained in the java.lang package, and cal­cu­la­tion results of this class are usually of the double data type.

Web Hosting
Hosting that scales with your ambitions
  • Stay online with 99.99% uptime and robust security
  • Add per­for­mance with a click as traffic grows
  • Includes free domain, SSL, email, and 24/7 support

How to perform different cal­cu­la­tions with Java Math

The best way to un­der­stand the func­tion­al­i­ty and syntax of the Java class Math is to use examples. It’s easier to un­der­stand the class and how it’s used in the context of in­di­vid­ual methods. Below, we’ve included a range of different cal­cu­la­tions 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 de­ter­min­ing 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 so­phis­ti­cat­ed cal­cu­la­tions that the Java class Math can do. For example, you can calculate powers as well. The method for cal­cu­lat­ing powers is called pow(). With this method, we need to first define a base and an exponent before carrying out the cal­cu­la­tion.

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 cal­cu­la­tions 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() Cal­cu­lates the square root
random() Returns a random double value
cbrt() Cal­cu­lates the cube root
log() Returns the natural logarithm of a double value
sin() Cal­cu­lates the sine of a double value
cos() Cal­cu­lates the cosine of a double value
tan() Cal­cu­lates 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
Go to Main Menu