Mathematical Functions in Java

In this tutorial, we will learn all the major mathematical functions in Java with examples.

Performing Mathematical Functions in Java

Java contains packages for doing mathematical functions. Every time we write the package Java.lang.Math, it calls mathematical functions automatically.

Few Mathematical Functions in Java:

They help us perform mathematical tasks in a much easier manner. Math class in Java helps in performing mathematical calculations like finding square, cube-root, maximum, absolute value, trigonometric values, etc. Let us a few mathematical functions:-


This helps us to find the square root of any positive number in Java. It returns a double type value. The negative number’s square root is always an imaginary number.


double n= Math.sqrt(9);

It returns a double type value which is the square root of the number 9.

Output is 3.0


We use this to find the value of any number raised to some power. This also always returns a                  double type value.

double n= Math.pow(3.0,2.0)

It returns a double type value. Here we have separated the two values by a comma. The number before the comma is the base. The number after the comma is the power raised to that number.

Output is 9.0


We use this function to find the minimum value out of the numbers given. The return value depends on the data type of the argument used. 

int n= Math.min(4,6);
Output is 4


We use this function to find the maximum of the two numbers. The return value depends on the data type of the arguments used.

int n= Math.max(3,4);
Output is 4


We use this to find value of natural log of the given argument. It always returns a double type value.

double x = Math.log(6.25);
Output is 1.832


We use this to return the absolute value of a number. This means finding only the magnitude of the number without any sign. The data type of the value we return depends on the arguments used.

int n = Math.abs(-8)
Output is 8


We use this to find the rounded value of any number. It always returns a double value. If the value of that number is less than x.5 then it returns x. If the value is more than x.5, then it returns a number next to x. Here x is any random number.

double n = Math.round(6.35);
Output is 6.0


We use this to find the rounded value of the number. It always returns the number down of the nearest integer. The return value is always of double type.

double n = Math.floor(-6.80);
Output is -7.0


We use this to find the rounded value next to the integer we have taken. It also returns double type values.

double n= Math.ceil(6.80);
Output is -6.0


Lastly, we see this function. We use this to find the exponential value of the number i.e. e^x. It also return double type values.

double d = Math.exp(6.25);
Output is 518.012


In conclusion, we can say that there are many mathematical functions provided to us by Java. They help us to all types of tasks from big calculations to complex problems. Therefore, we can easily perform our desired tasks. There are many more functions. We learned here about some of the most common and easiest functions. After this, it would be easier to work with more complex problems.

Leave a Reply

Your email address will not be published. Required fields are marked *