Java Math min() method with Examples

Getting the minimum of two given numbers (Java Math min method)

Just like the max method, we have got a min method as well, which, as you may have guessed as of now, returns the smaller of the two given values. This method is also overloaded. Let’s have a look at some of the programs now –

import java.lang.Math;
public class UnderstandingMath {
public static void main(String[] args) {
System.out.println(Math.min(12, 13));
}
}

The output of this program is 12, which is the smaller of the two values. We can try the same thing with two double values. Have a look at the below program –

import java.lang.Math;
public class UnderstandingMath {
public static void main(String[] args) {
System.out.println(Math.min(187.90878987956, 179.90906755434));
}
}

The output of the above program comes out to be 179.90906755434, which is desired. Now let’s try another program with float values this time.

import java.lang.Math;
public class UnderstandingMath {
public static void main(String[] args) {
System.out.println(Math.min(34.7986f, 67.8797f ));
}
}

So, the output of the above program is 34.7986 as desired. Let’s now try for the long-type data.

import java.lang.Math;
public class UnderstandingMath {
public static void main(String[] args) {
System.out.println(Math.min(276378271637812L, 478932387429908L));
}
}

The output of the above program comes out to be 276378271637812 as desired. So these were some variants for the min method, which returns the smaller of the two given number values.

Special cases –

  • If both arguments are the same values, the output is the same value.
  • If any one value is NaN, the output is also NaN.
  • For this method, the negative zero is considered less than the positive zero. So if the two arguments are negative zero and positive zero, then the output is negative zero.