Java Math class with Methods

Math Class in Java

Well, as the name says it all, the class Math helps us with some basic numeric operations like finding the square root of some number, finding logarithms, solving trigonometric functions, etc.

Java Math

So, whenever we need to perform some mathematical operations, like calculating the square root or cube root of some number or calculating the power of some number over another number, or when dealing with trigonometric functions, or logarithms, we can comfortably use the Math class.

First of all, whenever we want to use the Math class, we need to import it. So here is how we can import the Math class –

import java.lang.Math;

Fileds from the Math class –

Now let’s move on to see some of the fields in the Math class. Well, fields basically mean variables of some class.

There are two fields, namely E and PI. The E is the field containing a double value closer than any other value to e, and the PI is the field containing a double value closer than any other value to pi.

Let’s try printing the values of these fields in a program –

public class UnderstandingMath {
public static void main(String[] args) {
System.out.println(Math.E);
System.out.println(Math.PI);
}
}

So, if we try executing the program, we get an output like this –

2.718281828459045
3.141592653589793

So, we can use these values as and when required in our programs. For example, if we are creating a simple program

for calculating the area of a circle, then we will need the value of pi, which we can get from here as we did above.

Math Class Methods

MethodDescription
abs(double a)This method returns the absolute value of the given double number. The absolute value of a number is the distance of that number from the number 0 on the number line.
abs(float a)This method returns the absolute value of the given float data value.
abs(int a)This method returns the absolute value of the given integer number.
abs(long a)This method returns the absolute value of the given long value.
cbrt(double a)This method returns the cube root of the given double number
ceil(double a)This method returns the smallest double number greater than or equal to the given number and is equal to an integer. Ex – ceil of 18.4 is going to be 19.0 since it is the smallest double number, greater than the given number in this case.
cos(double a)This method returns the cosine of an angle.
floor(double a)This method returns the largest double number which is less than or equal to the given double value and is equal to an integer. Ex – floor of 16.7 is going to be 16.0 since it is the largest double number less than the given number.
max(int a,int b)This method returns the max value between the two given integers. I think this is self-explanatory.
max(double a double b)This method returns the max value between the two given double numbers.
max(float a, float b)This method returns the max value between the two given float values.
max(long a long b)This method returns the max value between the given long values.
min(int a, int b)This method returns the minimum value between the given integers.
min(double a double b)This method returns the minimum value between the given double values.
min(float a, float b)This method returns the minimum value between the given float values.
min(long a long b)This method returns the minimum value between the given long values.
pow(double a double b)This method returns the value of argument a to the power of argument b.
random()This method returns a double value greater than or equal to 0.0 and less than 1.0. As the name says, we are getting random values within the given range of 0.0 to 1.0, while 1.0 is not included.
round(double a)This method rounds the given double value to the nearest long number. Ex – the number 12.1234567891234 rounds to 12
round(float a)This method rounds the given float value to the nearest integer number. Ex – the number 18.89 rounds to 19
sin(double a)This method returns the sine of the given angle.
sqrt(double a)This method returns the rounded positive square root of the given double value.

Let’s now try doing some programs to implement these methods. Just remember that these methods are static. So, while calling them, we are going to access them with the class name itself.