Math class Methods Java

One of the important packages of Java is java.util. The classes of this package are known as utility classes. Literal meaning of utility is a service we use everyday. For example, Water, Power supply and Sewerage department etc are known as utility services. Similarly some classes exist which we use generally in every Program; they are known as utility classes – Date, Calendar, Math, Random etc.

Here, we discuss java.lang.Math class methods frequently used and rarely used separately.

All methods of Math class static methods so that you can call them with class name directly without creating object.

These methods used for simple arithmetic operations are self-explanatory and exist in many languages.

See the signature of Math class as defined in java.lang package.

public final class Math extends Object

Being final, Math class cannot be extended.

The Math class includes only two static final constants as defined here under.

public static final double E = 2.7182818284590452354; (Natural logarithm base)

public static final double PI = 3.14159265358979323846; (Circle circumference to diameter)

Following example uses Math class Methods frequently used.
public class MathClassMethods
  public static void main(String args[])
    System.out.println("PI constant value: " + Math.PI);  	
				// abs() returns always +ve value
    System.out.println("Math.abs(-5.8): " + Math.abs(-5.8));  	
    System.out.println("Math.abs(5.8): " + Math.abs(5.8));       
				// returns nearest rounded higher value
    System.out.println("Math.ceil(3.8): " + Math.ceil(3.8));                                    
    System.out.println("Math.ceil(3.1): " + Math.ceil(3.1));                                    
    System.out.println("Math.ceil(-3.1): " + Math.ceil(-3.1));                                   

				// returns nearest rounded lower value			
    System.out.println("Math.floor(3.8): " + Math.floor(3.8));
    System.out.println("Math.floor(-3.1): " + Math.floor(-3.1)); 

		                // returns rounded value. Higher value if mantissa is greater than 0.5 else lower value
    System.out.println("Math.round(3.8): " + Math.round(3.8)); 
    System.out.println("Math.round(3.3): " + Math.round(3.3));   
    System.out.println("Math.round(-3.3): " + Math.round(-3.3));

				// returns maximum of two numbers 
    System.out.println("Math.max(6.8, 2.4): " + Math.max(6.8, 2.4));      
				// returns minimum of two numbers 
    System.out.println("Math.min(6.8, 2.4): " + Math.min(6.8, 2.4));    

ssOutput screenshot of Math class methods frequently used.

Following example uses Math class Methods rarely used.
public class MathClassMethods
  public static void main(String args[])
		                // returns the power value, first parameter value raised by second parameter value
  System.out.println("\nMath.pow(4, 6): " + Math.pow(4, 6));    
				// returns random number
  System.out.println("Math.random(): " + Math.random());  
				// returns square root of value passed
  System.out.println("Math.sqrt(15.65): " + Math.sqrt(15.65));   
 	   	 	      // returns trigonometric values for parameter passed in radians
  System.out.println("Math.sin(50): " + Math.sin(50));        
  System.out.println("Math.cos(30): " + Math.cos(30));       
  System.out.println("Math.tan(20): " + Math.tan(20));       

ssOutput screenshot of Math class methods rarely used.

Other java.util package service classes

1. class Date
2. class Calendar
3. class GregorianCalendar
4. class Random
5. Timer and TimerTask
6. classes Observer and Observable
7. class StringTokenizer

Leave a Comment

Your email address will not be published.