Home »
Java programming language
Java StrictMath min() method with example
StrictMath Class min() method: Here, we are going to learn about the min() method of StrictMath Class with its syntax and example.
Submitted by Preeti Jain, on December 26, 2019
StrictMath Class min() method
Syntax:
public static float min(float f1 , float f2);
public static int min(int i1 , int i2);
public static long min(long l1 , long l2);
public static double min(double d1 , double d2);
- min() method is available in java.lang package.
- These methods are used to return the minimum one of both the given argument in the method or other words this method returns the smallest value of the given two arguments in the method.
- These methods don't throw an exception.
- These are static methods, it is accessible with the class name and, if we try to access these methods with the class object then we will not get any error.
Parameter(s):
- Method accepts two arguments of int / long / float / double that represents the values to find the minimum/smallest value.
Return value:
The return type of this method is int / long / float / double – it returns the minimum/smallest value from given two arguments.
Note:
- If we pass NaN as any of the arguments, method returns the NaN.
Example:
// Java program to demonstrate the example
// of min() method of StrictMath class
public class Min {
public static void main(String[] args) {
// variable declarations
float f1 = -0.0f;
float f2 = 0.0f;
float f3 = -0.6f;
float f4 = 124.58f;
int i1 = -0;
int i2 = 0;
long l2 = 0l;
long l3 = -2468l;
long l4 = 12458l;
double d1 = -0.0;
double d2 = 0.0;
// Display previous value of f1,f2,f3 and f4
System.out.println("f1: " + f1);
System.out.println("f2: " + f2);
System.out.println("f3: " + f3);
System.out.println("f4: " + f4);
// Display previous value of i1,i2
System.out.println("i1: " + i1);
System.out.println("i2: " + i2);
// Display previous value of l1,l2
System.out.println("l3: " + l3);
System.out.println("l4: " + l4);
// Display previous value of d1,d2
System.out.println("d1: " + d1);
System.out.println("d2: " + d2);
System.out.println();
System.out.println("min(float,float): ");
// Here , we will get (-0.0) because we are
// passing parameter whose value is (-0.0f,0.0f)
System.out.println("StrictMath.min(f1,f2): " + StrictMath.min(f1, f2));
// Here , we will get (0.0) and we are
// passing parameter whose value is (0.0f,124.58f)
System.out.println("StrictMath.min(f2,f4): " + StrictMath.min(f2, f4));
System.out.println();
System.out.println("min(int,int): ");
// Here , we will get (-0) because we are
// passing parameter whose value is (-0,0)
System.out.println("StrictMath.min(i1,i2): " + StrictMath.min(i1, i2));
System.out.println();
System.out.println("min(long,long): ");
// Here , we will get (0) and we are
// passing parameter whose value is (0l,12458l)
System.out.println("StrictMath.min(l2,l4): " + StrictMath.min(l2, l4));
System.out.println();
System.out.println("min(double,double): ");
// Here , we will get (-0.0) because we are
// passing parameter whose value is (-0.0,0.0)
System.out.println("StrictMath.min(d1,d2): " + StrictMath.min(d1, d2));
}
}
Output
f1: -0.0
f2: 0.0
f3: -0.6
f4: 124.58
i1: 0
i2: 0
l3: -2468
l4: 12458
d1: -0.0
d2: 0.0
min(float,float):
StrictMath.min(f1,f2): -0.0
StrictMath.min(f2,f4): 0.0
min(int,int):
StrictMath.min(i1,i2): 0
min(long,long):
StrictMath.min(l2,l4): 0
min(double,double):
StrictMath.min(d1,d2): -0.0