min() in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Overview

The Math.min() is a built-in method in Java that which accepts two parameters and returns the minimum of the two. The parameters can be of following types:

  • double
  • int
  • long
  • float

The return type of Math.min() method is same as that of the return types of its arguments.

Syntax of min() in Java

Here, number1 is the first argument and number2 is the second argument. The minimum of number1 and number2 is returned by Math.min() method.

The min() method in Java is known to be a static method. Hence, we can call it directly using the class name.

Various data types can be provided as arguments in Math.min() method. The syntax in such cases are:

1) Syntax using int

2) Syntax using long

3) Syntax using float

4) Syntax using double

Parameters of min() in Java

Parameters of Math.min() method must be one of the following types:

  • int
  • double
  • float
  • long

IMPORTANT: The parameters can be of different types.

Return Value of min() in Java

The return type of min() method would be similar to the data type of the arguments provided. The smallest of the two arguments is returned by this function.

One Negative and One Positive Number as Parameters

If a negative number and a positive number are provided as parameters to the min() function, the outcome will be a negative integer.

For instance, if the integers are -13 and 20 are provided as inputs, the result will be -13.

Negative numbers as input

If both arguments are negative, the number with higher magnitude is returned as the minimum.

For example, if -12.2 and -14.5 are provided as inputs, -14.5 will be returned as the minimum.

Two Similar Numbers as Parameters

If suppose the numbers are -13 and -13 are provided as inputs, then the result is -13.

NaN Values as Parameters

If either given value is NaN (that is Not a Number), the output generated will likewise be equal to NaN.

Example

Output:

More Examples of Java Math.min()

Let's go through a few examples to understand the function min() in Java.

Example 1:

Java Math.min()

Output:

Example 2:

Get the minimum value from an array using Math.min() method and a loop.

Output:

Explanation:

  • An array called arr is generated in the example given above. The first value of the array is initially stored in the attribute named smallest.
  • We've utilized the for loop to retrieve all of the array's items. Take note of the line, smallest = min (smallest, arr1[j]);.
  • The Math.min() function is responsible for comparing the value smallest to all of the array's items then returns the lowest number.

Example 3:

Find the minimum of two integers where one is positive, and the other is negative.

Output :

Example 4:

In the following example, on passing two negative numbers (of type int) as arguments to min() method, the negative number with higher magnitude is returned.

Output:

Example 5:

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.

Output:

Example 6:

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

Here, number1 is the first argument, and number2 is the second argument. The' Math. min () ' method returns the minimum of number1 and number2.

The min() method in Java is known as a static method. Hence, we can call it directly using the class name.

Various data types can be provided as arguments in Math.min() method. The syntax in such cases is:

1) Syntax using int

2) Syntax using long

3) Syntax using float

4) Syntax using double

:::

Parameters of min() in Java

Parameters of the Math.min() method must be one of the following types:

  • int
  • double
  • float
  • long

IMPORTANT: The parameters can be of different types.

Return Value of min() in Java

The return type of the min() method would be similar to the data type of the arguments provided. This function returns the smallest of the two arguments.

One Negative and One Positive Number as Parameters

If a negative and positive numbers are provided as parameters to the min() function, the outcome will be a negative integer.

For instance, if the integers are -13 and 20 are provided as inputs, the result will be -13.

Negative numbers as input

If both arguments are negative, the number with a higher magnitude is returned as the minimum.

For example, if -12.2 and -14.5 are provided as inputs, -14.5 will be returned as the minimum.

Two Similar Numbers as Parameters

If suppose the numbers are -13 and -13 are provided as inputs, then the result is -13.

NaN Values as Parameters

If either given value is NaN (Not a Number), the output generated will likewise be equal to NaN.

Example

Output:

More Examples of Java Math.min()

Let's review a few examples to understand the function min() in Java.

Example 1:

Java Math.min()

Output:

Example 2:

Get the minimum value from an array using the Math.min() method and a loop.

Output:

Explanation:

  • An array called arr is generated in the example given above. The first value of the array is initially stored in the attribute named smallest.
  • We've utilized the for loop to retrieve all of the array's items. Take note of the line, smallest = min (smallest, arr1[j]);.
  • The Math.min() function is responsible for comparing the value smallest to all of the array's items and then returns the lowest number.

Example 3:

Find the minimum of two integers where one is positive and the other is negative.

Output :

Example 4:

In the following example, on passing two negative numbers (of type int) as arguments to the min() method, the negative number with higher magnitude is returned.

Output:

Example 5:

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

Output:

Example 6:

Find the minimum of two float type positive numbers: In the following example, we will pass two numbers (of type float) as arguments to the min() method and find the smallest of the two float numbers.

Output:

Example 7:

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

Output:

:::

Conclusion

  • The Math.min() method in Java returns the minimum of its two input arguments.
  • The output is a number closer to -∞ in general. The outcome will be the same if all of the parameters have similar values.
  • The arguments in the function min in java can be of the following types: int( for ex: 2,6,81), double (for ex: 22.4, 3.5), float( for ex: 2.65f, 7.89f), and long(for ex: 10l, 2l).
  • The arguments of Math.min() method can be of different types.
  • In case any of the inputs to the Math.min() method is NaN, the same value, i.e. NaN, is returned as output.
  • If the arguments to the Math.min() method are negatives, the negative number with a higher magnitude is returned as output.

public static void main(String args[]) { //Two float type numbers given as input float num1 = 52.73f; float num2 = 50.95f; //print the minimum of two given numbers System.out.println(Math.min(num1, num2)); } }

**Output**:
```java
50.95f

Example 7:

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

Output:

:::

Conclusion

  • The Math.min() method in Java returns the minimum of its two input arguments.
  • The output is a number that is closer to -∞ in general. If all of the parameters have similar values, the outcome will be the same.
  • The arguments in the function min in java can be of the following types: int( for ex : 2,6,81), double (for ex : 22.4, 3.5), float( for ex: 2.65f, 7.89f), and long(for ex: 10l, 2l).
  • The arguments of Math.min() method can be of different types.
  • In case any of the inputs to Math.min() method is NaN, the same value i.e. NaN is returned as output.
  • If the arguments to the Math.min() method are negatives, the negative number with higher magnitude is returned as output.