Find Minimum of Two Numbers min() Java

java.lang.Math class comes with many methods to do simple basic numeric operations. One such one is min() method which returns the minimum of two numbers passed as parameter.

One advantage of Math class methods is that they are declared as static so that they can be called without the need of object creation.

Following is the Math class signature as defined in java.lang package.

public final class Math extends Object

min() method in Math class is 4 times overloaded accepting 4 different types of arguments.

  1. int min(int a, int b): Returns minimum of two integer values passed as parameter.
  2. long min(long a, long b): Returns minimum of two long values passed as parameter.
  3. float min(float a, float b): Returns minimum of two float values passed as parameter.
  4. double min(double a, double b): Returns minimum of two double values passed as parameter.

Observe, the above methods data type of the returned value is the same as parameters.

Following example on "Find Minimum of Two Numbers min() Java "illustrates of the above four methods.
public class MathFunctionDemo
{
  public static void main(String args[])
  {
   int a = 25, b = 50;			//  two integer values
   long c = 50, d = 20;                 // two long values
   float e = 25.5f, f = 10.4f;		//  two float values
   double g = 20.3, h = 50.2;		//  two double values

   System.out.println("Minimum of integer values 25 and 50 is " + Math.min(a, b));     
   System.out.println("Minimum of long values 50 and 20 is " + Math.min(c, d));     
   System.out.println("Minimum of float values 25.5 and 10.4 is " + Math.min(e, f));     
   System.out.println("Minimum of double values 20.3 and 50.2 is " + Math.min(g, h));     
  }
}

Screenshot of the above code when run.

Find Minimum of Two Numbers min() Java

Leave a Comment

Your email address will not be published.