LAST UPDATED: OCTOBER 16, 2020
Java Integer min() Method
Java min()
method is a part of the Integer
class of the java.lang
package and is specified by the Math.min()
method of the Math
class. This method is used to return the numerically smaller value(minimum value) of the two numbers passed as arguments.
If a positive and a negative number is passed then the negative value is returned but if both the numbers passed are negative then higher magnitude value is returned.
Syntax:
public static int min(int a, int b)
Parameters:
The parameters passed are the numeric values by the user to check for the minimum of the two numbers passed.
Returns:
The numerically lower value out of the two values passed as the parameter.
Example 1:
Here, one positive and one negative number is taken, hence the negative value is returned and in case of both negative, the value with higher magnitude is returned.
import java.lang.Integer;
public class StudyTonight
{
public static void main(String[] args)
{
int x = 5485;
int y = -3242;
int z = -5645;
// print the smaller number between x and y
System.out.println("Lower number is " + Integer.min(x, y));
// print the smaller number between y and z
System.out.println("Lower number is " + Integer.min(y, z));
}
}
Lower number is -3242
Lower number is -5645
Example 2:
Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent output.
import java.util.Scanner;
public class StudyTonight
{
public static void main(String[] args)
{
try
{
System.out.println("Enter the Two values: ");
Scanner sc= new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
//Print the smaller number between a and b
System.out.println("Smaller value is " + Integer.min(a, b));
}
catch(Exception e)
{
System.out.println("Exception occurred...");
}
}
}
Enter the Two values:
78 -98
Smaller value is -98
**************************
Enter the Two values:
-45 -65
Smaller value is -65
Live Example:
Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.