Signup/Sign In
LAST UPDATED: OCTOBER 15, 2020

Java Double min() Method

Java min() method is a part of the Double class of the java.lang package. This method returns the same result as by the Math.max() method of the Math class. This method is used to return the numerically smaller value(minimum value) of the two numbers passed as arguments.

If a positive and a negative number is passed then the negative value is returned but if both the numbers passed are negative then a higher magnitude value is returned.

Syntax:

public static double min(double a, double b)  

Parameters:

The parameters passed are the double values by the user to check for the minimum of the two numbers passed.

Returns:

The numerically lower double value out of the two values passed as the parameter.

Example 1:

Here, one positive and one negative number is taken, hence the negative value is returned and in the case of both negative, the value with higher magnitude is returned.

import java.lang.Double;
public class StudyTonight 
{  
    public static void main(String[] args) 
    {            
          double x = 5485.704;  
          double y = -3242.032; 
          double z = -5645.899;
               
          System.out.println("Lower number is " + Double.min(x, y));  // print the larger number between x and y 
          System.out.println("Lower number is " + Double.min(y, z));  // print the larger number between y and z
    }  
}


Lower number is -3242.032
Lower number is -5645.899

Example 2:

Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent output.

import java.util.Scanner; 

public class StudyTonight
{  
    public static void main(String[] args) 
    {  
        try
        {
           System.out.println("Enter the Two values: ");  
           Scanner sc= new Scanner(System.in);  
           double a = sc.nextDouble();  
           double b = sc.nextDouble();  
                                  
           System.out.println("Smaller value is " + Double.min(a, b)); //Print the larger number between a and b           
        }
        catch(Exception e)
        {
          System.out.println("Invalid Input!!");
        }         
    }  
} 


Enter the Two values:
78.88 65.63
Smaller value is 65.63
*************************
Enter the Two values:
87.98 -44.33
Smaller value is -44.33
**************************
Enter the Two values:
0x556 0x699
Invalid Input!!

Live Example:

Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.



About the author:
A Computer Science and Engineering Graduate(2016-2020) from JSSATE Noida. JAVA is Love. Sincerely Followed Sachin Tendulkar as a child, M S Dhoni as a teenager, and Virat Kohli as an adult.