Math Class

  • Math class provide predefined methods for mathematical operations.
  • We don’t need to create an object of the Math class to use it.
  • To access it , just type in Math & the corresponding method.
  • Math.abs() – returns the absolute value of its parameters.
  • Example:-
    class Myclass{
        public static void main(String args[]){
            int a = Math.abs(10);    //Output= 10
            int b = Math.abs(-20);   //Output= 20
         }
    }
  • Math.ceil() – rounds a floating point value up to the nearest integer value . The rounded value is returned as double.
  • double c = Math.ceil(7.342);       //output= 8.0
  • Math.floor() – rounds a floating point value down to the nearest integer values.
  • double c = Math.floor(7.343);     //output- 7.0
  • Math.max() – returns the largest of its parameters.
  • int m = Math.max(10,20);          //output=  20
  • Math.min() – returns the smallest of its parameters
  • int m = Math.min(10,20);         //output=  10
  • Math.pow() – takes two parameters & returns the first parameter raised to the power of the second parameter. Return type is double .
  • double p = Math.pow(2,3)         //output=  8.0
  • Other Methods –  Math.sqrt()  , Math.sin()  , Math.cos()  , Math.asin() , etc.