The min() method returns the smaller value among the specified arguments.
Example
Syntax of Math.min()
The syntax of the min() method is:
Math.min(arg1, arg2)
Here, min() is a static method. Hence, we are accessing the method using the class name, Math.
min() Parameters
The min() method takes two parameters.
- arg1/arg2 - arguments among which smaller value is returned
Note: The data types of the arguments should be either int, long, float, or double.
min() Return Value
- returns the minimum value among the specified arguments
Example 1: Java Math.min()
In the above example, we have used the Math.min() method with int, long, float, and double type arguments.
Example 2: Get a minimum value from an array
In the above example, we have created an array named arr. Initially, the variable min stores the first element of the array.
Here, we have used the for loop to access all elements of the array. Notice the line,
min = Math.min(min, arr[i])
The Math.min() method compares the variable min with all elements of the array and assigns the minimum value to min.