Parameters
The Math.min() method takes two parameters:
1. `a`: The first number to compare.
2. `b`: The second number to compare.
Both `a` and `b` can be of any numeric data type, such as int, long, float, or double. If the parameters have different data types, the method automatically performs necessary type conversions.
Return Value
The Math.min() method returns the minimum value between the two numbers passed as parameters. The return value has the same data type as the parameters. If the parameters have different data types, the method returns the minimum value in the larger data type.
For example, if you pass an int & a double to Math.min(), the method will return a double value.
Example
Let's look at a few examples to understand how to use the Math.min() method in Java:
Example 1
public class MathMinExample {
public static void main(String[] args) {
int a = 10;
int b = 20;
int minValue = Math.min(a, b);
System.out.println("The minimum value is: " + minValue);
}
}

You can also try this code with Online Java Compiler
Run Code
In this example, we have two integer variables, `a` and `b`, with values 10 and 20, respectively. We use the Math.min() method to find the minimum value between `a` and `b` and store the result in the `minValue` variable. Finally, we print the minimum value using System.out.println().
Output:
The minimum value is: 10
As expected, the Math.min() method returns 10, which is the minimum value between 10 & 20.
Example 2
public class MathMinExample1 {
public static void main(String[] args) {
double x = 3.14;
double y = 2.71;
double minValue = Math.min(x, y);
System.out.println("The minimum value is: " + minValue);
}
}

You can also try this code with Online Java Compiler
Run Code
Output:
The minimum value is: 2.71
In this example, we compare two double values, `x` and `y`, using Math.min(). The method returns the minimum value, which is 2.71.
Example 3
public class MathMinExample2 {
public static void main(String[] args) {
long a = 1000000000L;
int b = 1000000;
long minValue = Math.min(a, b);
System.out.println("The minimum value is: " + minValue);
}
}

You can also try this code with Online Java Compiler
Run Code
Output:
The minimum value is: 1000000
Here, we compare a long value, `a`, with an int value, `b`. The Math.min() method automatically performs type conversion and returns the minimum value as a long, which is 1000000.
Example 4
public class MathMinExample3 {
public static void main(String[] args) {
int a = -10;
int b = 5;
int c = 0;
int minValue = Math.min(a, Math.min(b, c));
System.out.println("The minimum value is: " + minValue);
}
}

You can also try this code with Online Java Compiler
Run Code
Output
The minimum value is: -10
In this example, we find the minimum value among three integers: 'a`, `b`, and `c`. We use nested Math.min() calls to compare `b` and `c` first and then compare the result with `a`. The method returns the minimum value, which is -10.
Frequently Asked Questions
Can Math.min() be used with more than two numbers?
Yes, you can use nested Math.min() calls to find the minimum among multiple numbers.
Does Math.min() work with negative numbers?
Yes, Math.min() works with negative numbers & correctly identifies the minimum value.
Can Math.min() compare numbers of different data types?
Yes, Math.min() automatically performs type conversion when comparing numbers of different data types.
Conclusion
In this article, we discussed the Math.min() method in Java, which is used to find the minimum value between two numbers. We learned about its syntax, parameters, and return value and saw examples of how to use it effectively in code. Math.min() is a simple and powerful method that can be used in many situations to compare and find the minimum value among numbers.
You can also check out our other blogs on Code360.