Java Math min() Method
The min() method of Math class returns the smaller of two arguments. The arguments can be of double, float, int or long data type.
Syntax:
- public static double min (double a, double b)
- public static float min (float a, float b)
- public static int min (int a, int b)
- public static long min (long a, long b)
Parameters:
The parameters ‘a’ and ‘b’ represent the two values.
Return Value:
The min() method returns the smaller of a and b.
- It returns the result of same value, if the arguments have same value
- It returns NaN, if either value is NaN.
- It returns negative zero, if one argument is positive zero and the other is negative zero.
Example 1:
public class JavaMathMinExample1 {
public static void main(String[] args) {
int a = 67;
int b= 56;
//returns the smaller number between a and b
System.out.println("The smaller number between "+a+" and "+ b+ "
is : "+Math.min(a,b));
}
}
Output:
The smaller number between 67 and 56 is : 56
Example 2:
public class JavaMathMinExample2 {
public static void main(String[] args) {
Double a = 67.0d;
Double b= Double.NaN;
//returns NaN, if either value is NaN
System.out.println("The smaller number between "+a+" and "+ b+ "
is : "+Math.min(a,b));
}
}
Output:
The smaller number between 67.0 and NaN is : NaN
Example 3:
public class JavaMathMinExample3 {
public static void main(String[] args) {
float a = 0f;
float b=-0f;
//returns negative zero, if one argument is positive zero and the
other is negative zero
System.out.println("The smaller number between "+a+" and "+ b+ "
is : "+Math.min(a,b));
}
}
Output:
The smaller number between 0.0 and -0.0 is : -0.0
Example 4:
public class JavaMathMinExample4 {
public static void main(String[] args) {
Double a = 67.0d;
Double b= 67.0d;
//returns the result of same value, if the arguments have same value
System.out.println("The smaller number between "+a+" and "+ b+ "
is : "+Math.min(a,b));
}
}
Output:
The smaller number between 67.0 and 67.0 is : 67.0
