Updated April 1, 2023

## Introduction to Java min() Function

In Java, min() is an inbuilt method that returns the minimum value of two numbers. It is inherited from the package java.lang.math, and the arguments are taken in the types double, int, long and float. Moreover, this method can be overloaded, and there are certain conditions for implementing this method. It will be discussed in the section where the working is explained. In addition to that, syntax and examples of the min() method can be seen in the below sections.

**Syntax :**

As already discussed, different data types such as int, float, double, and long can be used in this method. Following are the corresponding syntaxes of these different data types of method min().

`public static int min(int num1, int num2) //syntax of min with datatype int`

`public static long min(long num1, long num2) //syntax of min with datatype long`

`public static float min(float num1, float num2) //syntax of min with datatype float`

`public static double min(double num1, double num2) //syntax of min with double`

**Parameters**: num1 and num2 of different datatypes from which the minimum value among them is returned.

**Return value**: Minimum of two numbers passed as arguments will be returned, and the datatype of the result will be the same as the arguments.

### How does the min() method work in Java?

**1.** If a negative number and a positive number are passed as the method’s arguments, then the result generated will be negative.

**Example:**If the numbers -32 and 21 are given as arguments, then -32 will be returned.

**2.** If both parameters passed as the method’s arguments are negative, then the result generated will be the one with a higher magnitude. That is, it will be closer to the –ve(negative) infinity.

**Example:** If the numbers -32 and -21 are given as arguments, then -32 will be returned.

**3.** If both parameters passed as the method’s arguments are the same, then the result generated will be that same value.

**Example:** If the numbers -32 and -32 are given as arguments, then -32 will be returned.

**4.** If NaN(Not a Number) is either value, the result generated will also be NaN.

### Examples to Implement Java min() method

Below are the examples of the Java min() method:

#### Example #1

Java program to find the minimum of two int type positive numbers.

**Code:**

```
public class MinExample {
public static void main(String[] args) {
// Declare two numbers of
```__int__ type
int x = 41;
int y = 67;
// print the minimum number among x and y
System.out.println("Minimum among x="+x+" and y="+y+ " is: " + Math.min(x, y));
}
}

**Output:**

In this program, two positive numbers, 41 and 67, are declared, and the minimum among them 41 is found using the min() method.

#### Example #2

Java program to find the minimum of two int type numbers where one is positive, and the other is negative.

**Code:**

```
public class MinExample {
public static void main(String[] args) {
// Declare two numbers of
```__int__ type
int x = 41;
int y = -67;
// print the minimum number among x and y
System.out.println("Minimum among x="+x+" and y="+y+ " is: " + Math.min(x, y));
}
}

**Output:**

In this program, a positive number 41 and a negative number -67 is declared. The minimum among them, -67, which is closer to the negative infinity, is found using the min() method.

#### Example #3

Java program to find the minimum of two int type negative numbers.

**Code:**

```
public class MinExample {
public static void main(String[] args) {
// Declare two numbers of
```__int__ type
int x = -41;
int y = -67;
// print the minimum number among x and y
System.out.println("Minimum among x="+x+" and y="+y+ " is: " + Math.min(x, y));
}
}

**Output:**

In this program, two negative numbers, -41 and -67, are declared. The minimum among them, -67, which is closer to the negative infinity, is found using the min() method.

#### Example #4

Java program to find the minimum of two double type positive numbers.

**Code:**

```
public class MinExample {
public static void main(String[] args) {
// Declare two numbers of double type
double x = 26.11;
double y = 26.12;
// print the minimum number among x and y
System.out.println("Minimum among x="+x+" and y="+y+ " is: " + Math.min(x, y));
}
}
```

**Output:**

Unlike the above programs, here, two positive numbers, 26.11 and 26.12, of double type are declared. But, the minimum among them, 26.11, is found using the min() method similar to the above programs.

#### Example #5

Java program to find the minimum of two float type positive numbers.

**Code:**

```
public class MinExample {
public static void main(String[] args) {
// Declare two numbers of float type
float x = 26.11f;
float y = 26.12f;
// print the minimum number among x and y
System.out.println("Minimum among x="+x+" and y="+y+ " is: " + Math.min(x, y));
}
}
```

**Output:**

Here, two positive numbers, 26.11f and 26.12f of float type, are declared. The minimum among them, 26.11, is found using the min() method.

#### Example #6

Java program to find the minimum of two user input numbers.

**Code:**

```
import java.util.Scanner;
public class MinExample {
public static void main(String[] args) {
System.out.println("Enter two numbers from which the minimum has to be found: ");
//read input numbers from the user
Scanner in= new Scanner(System.in);
//store first number in x
int x = in.nextInt();
//store second number in y
int y = in.nextInt();
in.close();
// print the minimum number among x and y
System.out.println("Minimum among x="+x+" and y="+y+ " is: " + Math.min(x, y));
}
}
```

**Output:**

In this program, two numbers are asked to input by the user. As you can see, the numbers are given as 32 and 57, from which 32 is returned as the minimum number.

**What will happen if two numbers are given the same?**

It can be seen that the same number will be returned as a result.

### Recommended Articles

This is a guide to Java min(). Here we discuss the Introduction to Java min() method and its Examples along with Code Implementation. You can also go through our other suggested articles to learn more –