class Math Java


Introduction

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.

public class MathMethods
{
  public static void main(String args[])
  {
     System.out.println("PI value: " + Math.PI);  	
				// abs() method gives a positive value
   System.out.println("Math.abs(-4.7): " + Math.abs(-4.7)); 	
   System.out.println("Math.abs(4.7): " + Math.abs(4.7));           
				// rounds to a nearest higher value
   System.out.println("Math.ceil(4.7): " + Math.ceil(4.7));                                    
   System.out.println("Math.ceil(2.1): " + Math.ceil(2.1));                                    
   System.out.println("Math.ceil(-2.1): " + Math.ceil(-2.1));                                   
			
  System.out.println("Math.floor(2.9): " + Math.floor(2.9));
  System.out.println("Math.floor(-2.9): " + Math.floor(-2.9)); 
		// rounds as we do in maths; >= 0.5 rounds to higher value else to the lower
  System.out.println("Math.round(2.7): " + Math.round(2.7)); 
  System.out.println("Math.round(2.3): " + Math.round(2.3));   
  System.out.println("Math.round(-2.8): " + Math.round(-2.8));
				// gives maximum of the two 
  System.out.println("Math.max(3, 6.4): " + Math.max(3, 6.4));       
				// gives minimum of the two 
  System.out.println("Math.min(3.2, 6.4): " + Math.min(3.2, 6.4));     
		// gives the power value, first value raised by second value
  System.out.println("Math.pow(3, 4): " + Math.pow(3, 4));         
				// gives a rondom number
  System.out.println("Math.random(): " + Math.random());       
				// returns the square root
  System.out.println("Math.sqrt(24.87): " + Math.sqrt(24.87));        
 	   	 	      // gives trigonometric value (parameter in radians)
  System.out.println("Math.sin(40): " + Math.sin(40));        
  System.out.println("Math.cos(40): " + Math.cos(40));       
  System.out.println("Math.tan(40): " + Math.tan(40));       
 }
}


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 Comment

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