![]() |
VOOZH | about |
The Java.lang.math.min() function is an inbuilt function in java that returns the minimum of two numbers. It supports the following primitive data types: int, long, float, double.
Behaviour Notes:
Example:
11.0
static dataType min(dataType a, dataType b)
Parameters:
Both parameters must be of the same primitive type.
Return value: Returns the minimum of the two values. The return type is the same as the argument type.
This short example demonstrates how Math.min() works with floating-point numbers.
12.123
Explanation: Two double values are passed to Math.min(). The method compares both values and returns the smaller one.
Since 12.123 < 12.456, the output is 12.123.
The behavior of Math.min() is shown, when one value is negative.
-23
Explanation: The method compares a positive and a negative integer. Negative numbers are always smaller than positive numbers.
Therefore, -23 is returned as the minimum value.
Math.min() compares two negative values.
-25
Explanation: Both integers passed to Math.min() are negative. The value with the greater negative magnitude is considered smaller.
Hence, -25 is returned as it is less than -23.
If Math.min() is used frequently, you can statically import the method to avoid repeated class qualification.
3
Explanation: The min() method is statically imported from java.lang.Math. This allows calling min() directly without the Math class name.
The method compares 3 and 4 and returns 3.