You can carry out simple cal­cu­la­tions with Java Math. It has different methods that cover log­ar­ithms and tri­go­no­metry, as well as all of the basics. The syntax is com­par­at­ively 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­ar­ately and has numerous methods which we’ll cover in more detail later on in this article.

The Math class is not in­stan­ti­ated, and its methods are only accessed stat­ic­ally. The two constants of the class are also static: Euler’s number (ap­prox­im­ately 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­im­ately π = 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
The hosting your website deserves at an un­beat­able price
  • Loading 3x faster for happier customers
  • Rock-solid 99.99% uptime and advanced pro­tec­tion
  • Only at IONOS: up to 500 GB included

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

The best way to un­der­stand the func­tion­al­ity 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­vidu­al 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­term­in­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 soph­ist­ic­ated 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
13c5abf9d4e693b8376239553b263482

0d45cf8c24766a34961a4c1242d9dfed

f852018dac38d5c1d84b9ed3431ae6dc

Go to Main Menu