Minimum or maximum value between two numbers in java

The code below show you how to use the Math.min() and Math.max() method call the get the minimum and maximum value between two numbers. As other method in the Math class these methods also overloaded to accept many types of primitive data.

Below is the example code of getting maximum or minimum value between two numbers in java.

package com.java.connect.math;
 
//Minimum or maximum value between two numbers in java
public class MaxMinValueExample {
    public static void main(String[] args) {
 
        Double number1 = 100.0D;
        Double number2 = 200.0D;
 
        int max1 = Math.max(number1.intValue(), number2.intValue());
        int min1 = Math.min(number1.intValue(), number2.intValue());
        System.out.println("The Max value in int   : " + max1);
        System.out.println("The Min value in int   : " + min1);
 
        long max2 = Math.max(number1.longValue(), number2.longValue());
        long min2 = Math.min(number1.longValue(), number2.longValue());
        System.out.println("The Max value in long  : " + max2);
        System.out.println("The Min value in long  : " + min2);
 
        double max3 = Math.max(number1, number2);
        double min3 = Math.min(number1, number2);
        System.out.println("The Max value in double: " + max3);
        System.out.println("The Min value in double: " + min3);
 
        float max4 = Math.max(number1.floatValue(), number2.floatValue());
        float min4 = Math.min(number1.floatValue(), number2.floatValue());
        System.out.println("The Max value in float : " + max4);
        System.out.println("The Min value in float : " + min4);
    }
}

The output of the above example will be as:

The Max value in int   : 200
The Min value in int   : 100
The Max value in long  : 200
The Min value in long  : 100
The Max value in double: 200.0
The Min value in double: 100.0
The Max value in float : 200.0
The Min value in float : 100.0