class Math Java

class Math Java


The java.lang.Math class includes many methods with which many simple arithmetic operations can be done like finding the square root, rounding, trigonometric and logarithm functions. As these methods are defined as static, they can be used directly by the programmer without the trouble of creating an object. Following is the class signature as defined in the java.lang package.

public final class Math extends Object

As the class is final, it cannot be extended (like String class, you know earlier).

The Math class includes only two constants as defined hereunder.

public static final double E = 2.7182818284590452354; (Base of natural logarithms)

public static final double PI = 3.14159265358979323846; (ratio of circumference to the diameter of a circle)

Following table gives some important static methods of "class Math Java" and their purpose.
Method Legal Parameters What For
abs(x) int, long, float, double Gives the absolute value of x
acos(x) double Gives the arccosine of x
exp(x) double Gives the exponential value to the power of x
ceil(x) double Gives the minimum value but greater than x
floor(x) double Gives the maximum value but less than x
log(x) double Gives the natural log value of x
max(x,y) int, long, float, double Gives the maximum of x and y
min(x,y) int, long, float, double Gives the minimum of x and y
pow(x,y) double Gives a value x raised to y
random() Gives a random number between 0.0 to 1.0
rint(x) double Gives a double value nearer to x
round(x) float, double Gives the nearer integer to x
sqrt(x) double Gives the square root of x

Note: Also available asin(x), atan(x), sin(x), cos(x) and tan(x) etc. The parameter of trigonometric functions is in radians.

The next program uses some methods of Math class.

class Math Java
Output screenshot on class Math Java

random() method generates random number. For more on random number generation refer java.util package.

Leave a Reply

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