Java Math

Java has math class which have many methods in it which makes it easy to do mathematical tasks on numbers.

So, what are the types of mathematical functions we come across daily?

Maximum: if there are two numbers and we must compare which is the highest among the two we need the implementation of the maximum class.

Code:

public class Main{
    public static void main(String[]args){
        System.out.println(Math.max(4,5));
    }
}

Minimum: if there are two numbers and we must compare which is the lowest among the two we need the implementation of the minimum class.

public class Main{
    public static void main(String[]args){
        System.out.println(Math.min(4,3));
    }
}

Square root: If we have a perfect square numbers like 4, 16, 36… we are going to implement this sqrt class and get the perfect square for the desired number..

public class Main{
    public static void main(String[]args){
        System.out.println(Math.sqrt(16));
    }
}

Abs: this class returns a negative number to a positive value.

public class Main{
    public static void main(String[]args){
        System.out.println(Math.abs(-16.021));
    }
}

Scroll to top