Java Integer.min() – Examples

In this tutorial, we will learn about Java Integer.min() method, and learn how to use this method to get the minimum of given two integer values, with the help of examples.

min()

Integer.min(a, b) returns the lesser of two integer values: a and b.

Syntax

The syntax of min() method with two integers for comparison as parameters is

</>
Copy
Integer.min(int a, int b)

where

ParameterDescription
aThe first integer to compare.
bThe second integer to compare.

Returns

The method returns value of type int.

Example 1 – min(a, b) : a<b

In this example, we will take two integers in a and b such that a is less than b. We will find the minimum of a and b using Integer.min() method. Since a is less than b , min() should return the value of a.

Java Program

</>
Copy
public class Example {
	public static void main(String[] args){
		int a = 5;
		int b = 7;
		int result = Integer.min(a, b);
		System.out.println("Result of min("+a+", "+b+") = " + result);
	}
}

Output

Result of min(5, 7) = 5

Example 2 – min(a, b) : a=b

In this example, we will take two integers in a and b such that a is equal to b in value. We will find the minimum of a and b using Integer.min() method. Since a equals bin value, min() returns the value of a or b.

Java Program

</>
Copy
public class Example {
	public static void main(String[] args){
		int a = 5;
		int b = 5;
		int result = Integer.min(a, b);
		System.out.println("Result of min("+a+", "+b+") = " + result);
	}
}

Output

Result of min(5, 5) = 5

Example 3 – min(a, b) : a>b

In this example, we will take two integers in a and b such that a is greater than b. We will find the minimum of a and b using Integer.min() method. Since a is greater than b , min() should return the value of b.

Java Program

</>
Copy
public class Example {
	public static void main(String[] args){
		int a = 7;
		int b = 5;
		int result = Integer.min(a, b);
		System.out.println("Result of min("+a+", "+b+") = " + result);
	}
}

Output

Result of min(7, 5) = 5

Conclusion

In this Java Tutorial, we have learnt the syntax of Java Integer.min() method, and also how to use this method with the help of Java example programs.