What is Math.min() in Java?

The min() function returns the smallest value among the two numbers sent as a parameter.

Figure 1 shows a visual representation of the min() function.

Figure 1: Visual representation of min() function

Syntax

type min(type num-1, type num-2)

Parameter

The min() function takes two numbers as a parameter.

The numbers can be int, double, float, or long.

Return value

The min() function returns the smallest value from the two numbers sent as a parameter.

Code

class JAVA {
public static void main( String args[] ) {
// two positive numbers
System.out.println("Minimum between 9 and 8:");
System.out.println(Math.min(9,8));
//one positive and the other negative
System.out.println("Minimum between -9 and 8:");
System.out.println(Math.min(-9,8));
// both negative numbers
System.out.println("Minimum between -9 and -10:");
System.out.println(Math.min(-9,-10));
// both double numbers
System.out.println("Minimum between 12.234 and 1.2345:");
System.out.println(Math.min(12.234,1.2345));
// one int and the other double
System.out.println("Minimum between 12.234 and 1:");
System.out.println(Math.min(12.234,1));
}
}