Math class in Java with example

Math class is one of the most important class in Java defined in java.lang package. It comprises of several methods which can be used for basic mathematical operations like absolute value, minimum of given numbers, square root of given number, maximum of given numbers, trigonometric functions, logarithmic functions etc.

MATH CLASS IN Java

Let us understand most commonly used methods defined in java.lang.Math class in detail.

Methods

Method Description Example
static double sqrt(double a) Returns the correctly rounded positive square root of a double value.  Click Here
static double cbrt(double a) Returns the cube root of a double value.  Click Here
static double ceil(double a) Returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer.  Click Here
static double floor(double a) Returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer.  Click Here
static double pow(double a, double b) Returns the value of the first argument raised to the power of the second argument.  Click Here
static double random() Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0  Click Here
static int abs(int a) Returns the absolute value of an  int value.  Click Here
static long abs(long a) Returns the absolute value of a  long value.  Click Here
static int max(int a, int b)  Returns the greater of two int values.  Click Here
static long min(long a, long b) Returns the smaller of two long values.  Click Here
static double signum(double d) Returns the signum function of the argument; zero if the argument is zero, 1.0 if the argument is greater than zero, -1.0 if the argument is less than zero.  Click Here

static double sqrt(double a)

This method returns the correctly rounded positive square root of a double value.

Example

public class MathExample {
	public static void main(String str[]){
		double value = 16.00;
		double squareRootValue = Math.sqrt(value);
		System.out.println(squareRootValue);
	}
}

Output

4.0

static double cbrt(double a)

This method returns the cube root of a double value.

public class MathExample {
	public static void main(String str[]){
		double value = 64.00;
		double cubeRootValue = Math.cbrt(value);
		System.out.println(cubeRootValue);
	}
}

Output

4.0

static double ceil(double a)

This method returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer.

public class MathExample {
	public static void main(String str[]){
		double value = -64.80;
		double ceilValue = Math.ceil(value);
		System.out.println(ceilValue);
	}
}

Output

-64.00

static double floor(double a)

This method returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer.

public class MathExample {
	public static void main(String str[]){
		double value = -64.80;
		double floorValue = Math.floor(value);
		System.out.println(floorValue);
	}
}

Output

-65.00

static double pow(double a, double b)

This method returns the value of the first argument raised to the power of the second argument.

public class MathExample {
	public static void main(String str[]){
		double value = 10.00;
		int power = 2;
		double powerValue = Math.pow(value, power);
		System.out.println(powerValue);
	}
}

Output

100.00

static double random()

This method returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0

public class MathExample {
	public static void main(String str[]){
		double randomValue = Math.random();
		System.out.println(randomValue);
	}
}

Output

0.030615484496316014

static int abs(int a)

This method returns  the absolute value of an  int value.

public class MathExample {
	public static void main(String str[]){
		int negativeValue = -10;
		int absValue = Math.abs(negativeValue);
		System.out.println(absValue);
	}
}

Output

10

static long abs(long a)

This method returns  the absolute value of an  int value.

public class MathExample {
	public static void main(String str[]){
		long negativeValue = -10;
		long absValue = Math.abs(negativeValue);
		System.out.println(absValue);
	}
}

Output

10

static int max(int a, int b)

This method returns the greater of two int values.

public class MathExample {
	public static void main(String str[]){
		int value1 = 10;
		int value2 = 11;
		int maxValue = Math.max(value1,value2);
		System.out.println(maxValue);
	}
}

Output

11

static long min(long a, long b)

This method returns the smallest of two long values.

public class MathExample {
	public static void main(String str[]){
		long value1 = 10;
		long value2 = 11;
		long minValue = Math.min(value1,value2);
		System.out.println(minValue);
	}
}

Output

10

static double signum(double d)

Returns the signum function of the argument; zero if the argument is zero, 1.0 if the argument is greater than zero, -1.0 if the argument is less than zero.

public class MathExample {
	public static void main(String str[]){
		double negativeValue = Math.signum(-10);
		System.out.println("Signum of negativeValue : "+negativeValue);
		
		double positiveValue = Math.signum(11);
		System.out.println("Signum of positiveValue : "+ positiveValue);
		
		double zero = Math.signum(0);
		System.out.println("Signum of zero : "+ zero);
	}
}

Output

Signum of negativeValue : -1.0
Signum of positiveValue : 1.0
Signum of zero : 0.0

In this article, we learnt various methods available in Math class defined in java.lang package and how to use it in programming.

Article tagged as
Author
Author: Amit Gupta
Published On: 30/11/2015
Last revised On: 02/12/2015
View all articles by Amit Gupta

Share this post

Comments

Comments
comments powered by Disqus

Navigation

Social Media