Java Math.min()

min() accepts two numbers as arguments, and returns the smaller of the two values.

Following is the syntax of min() method.

</>
Copy
int result = min(int a, int b)
long result = min(long a, long b)
float result = min(float a, float b)
double result = min(double a, double b)

The datatype of return value is based on the datatype of arguments.

We shall learn about min() method in detail with the help of examples.

Example 1 – Math.min(int, int)

In the following example, we use will pass two integers as arguments to min() method and find the smallest of the two integer numbers.

Java Program

</>
Copy
public class MathExample {
	public static void main(String[] args) {
		int a = 10;
		int b = 5; 
		int result = Math.min(a, b);
		System.out.println(result);
	}
}

Output

5

Example 2 – Math.min(long, long)

In the following example, we use will pass two long numbers as arguments to min() method and find the smallest of the two numbers.

Java Program

</>
Copy
public class MathExample {
	public static void main(String[] args) {
		long a = 1099859955445L;
		long b = 58978542L; 
		long result = Math.min(a, b);
		System.out.println(result);
	}
}

Output

58978542

Example 3 – Math.min(float, float)

In the following example, we use will pass two floating point numbers as arguments to min() method and find the smallest of the two float numbers.

Java Program

</>
Copy
public class MathExample {
	public static void main(String[] args) {
		float a = 10.9985F;
		float b = 5.89F; 
		float result = Math.min(a, b);
		System.out.println(result);
	}
}

Output

5.89

Example 4 – Math.min(double, double)

In the following example, we use will pass two numbers (of type double) as arguments to min() method and find the smallest of the two double numbers.

Java Program

</>
Copy
public class MathExample {
	public static void main(String[] args) {
		double a = 10.9985;
		double b = 585566.89963333; 
		double result = Math.min(a, b);
		System.out.println(result);
	}
}

Output

10.9985

Example 5 – Math.min(x, y) – x or y as NaN

In the following example, we use will pass NaN for one of the two arguments. If any of the arguments to min() method is NaN, then the return value should be NaN.

Java Program

</>
Copy
public class MathExample {
	public static void main(String[] args) {
		double a = 10.9985;
		double b = Double.NaN; 
		double result = Math.min(a, b);
		System.out.println(result);
	}
}

Output

NaN

Example 6 – Math.min(float, int)

If you provide arguments of different datatype, then the lower datatype is promoted to higher datatype, and smallest value with the respective datatype is returned.

Java Program

</>
Copy
public class MathExample {
	public static void main(String[] args) {
		int a = 5;
		float b = 10.2F; 
		float result = Math.min(a, b);
		System.out.println(result);
	}
}

Output

5.0

In this example, among int and float, float is higher datatype and hence, a is promoted to float. min(int, float) becomes min(float, float).

Conclusion

In this Java Tutorial, we learned about Java Math.min() function, with example programs.