Home >>Java Math Methods >Java Math.min() method

Java Math.min() method

Java Math.min() method

The math.min() method is an inbuilt Java method which is used to return Minimum or Lowest value from the two arguments given. The arguments in int, float, double, and long are taken up.

Syntax

public static int min(int a, int b)  
public static double min(double a, double b)  
public static long min(long a, long b)  
public static float min(float a, float b)  

Parameters

a: It is used for a first value

b: It is used for a second value

Returns

This method is used to returns minimum of two numbers.

  1. This method will return negative argument if we give positive and negative value as argument.
  2. If we have all negative values as arguments, the resulting number with the greater magnitude will be returned.
  3. If the arguments are not number(NaN), then will be returned by this method.

Java Math.min() Method Example 1


public class MyClass   
{  
    public static void main(String args[])  
    {  
        int a = 16;  
        int b = 35;  
        System.out.println(Math.min(a, b));  
    }  
}  

Output:
16

Java Math.min() Method Example 2


public class MyClass   
{  
    public static void main(String args[])  
    {  
        double a = 12.05;  
        double b = -23.34;  
        System.out.println(Math.min(a, b));  
    }  
}  

Output:
-23.34