Java Double Min Method

JavaJavaBeginner
Practice Now

Introduction

In Java, the Double.min() method is used to find the smallest of two double values passed as arguments. It returns the numerically lower value of the two values passed. This lab will teach you how to use the Double.min() method through easy-to-understand steps with examples.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/exceptions("`Exceptions`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/user_input("`User Input`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/comments("`Comments`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117625{{"`Java Double Min Method`"}} java/classes_objects -.-> lab-117625{{"`Java Double Min Method`"}} java/class_methods -.-> lab-117625{{"`Java Double Min Method`"}} java/exceptions -.-> lab-117625{{"`Java Double Min Method`"}} java/modifiers -.-> lab-117625{{"`Java Double Min Method`"}} java/oop -.-> lab-117625{{"`Java Double Min Method`"}} java/packages_api -.-> lab-117625{{"`Java Double Min Method`"}} java/user_input -.-> lab-117625{{"`Java Double Min Method`"}} java/wrapper_classes -.-> lab-117625{{"`Java Double Min Method`"}} java/identifier -.-> lab-117625{{"`Java Double Min Method`"}} java/arrays -.-> lab-117625{{"`Java Double Min Method`"}} java/comments -.-> lab-117625{{"`Java Double Min Method`"}} java/data_types -.-> lab-117625{{"`Java Double Min Method`"}} java/operators -.-> lab-117625{{"`Java Double Min Method`"}} java/output -.-> lab-117625{{"`Java Double Min Method`"}} java/strings -.-> lab-117625{{"`Java Double Min Method`"}} java/variables -.-> lab-117625{{"`Java Double Min Method`"}} java/system_methods -.-> lab-117625{{"`Java Double Min Method`"}} end

Creating a Java class

Open your terminal and create a new file named DoubleMinMethod.java within your project directory using the following command:

touch DoubleMinMethod.java

Next, open your DoubleMinMethod.java file with your preferred text editor.

Importing the required package

To use the Double.min() method, you have to import the java.lang.Double package by adding the following line of code at the beginning of your DoubleMinMethod.java file:

import java.lang.Double;

Defining the main method

Add the main method to your DoubleMinMethod.java file:

public class DoubleMinMethod {
    public static void main(String[] args) {
        // Your code here
    }
}

Using the Double.min() method

In the main() method of your DoubleMinMethod.java file, create two double variables a and b, and set their values as per your choice:

double a = 3.14;
double b = 2.71;

Next, use the Double.min() method to find the minimum value between a and b and store it in another double variable called result:

double result = Double.min(a, b);

Displaying the result

Print the result using the System.out.println() method:

System.out.println("The minimum value is " + result);

Compiling and running the program

Save the changes to your DoubleMinMethod.java file and compile it using the following command:

javac DoubleMinMethod.java

If there are no errors in your code, the compiler will generate a .class file. You can run the program by typing the following command in the terminal:

java DoubleMinMethod

You should see the result of the Double.min() method displayed in the terminal.

Example implementation

Here is an example program that prompts the user to enter two double values and then finds the minimum value between them:

import java.util.Scanner;
import java.lang.Double;

public class DoubleMinMethod {
    public static void main(String[] args) {
        try {
            Scanner sc = new Scanner(System.in);

            System.out.print("Enter the first number: ");
            double a = sc.nextDouble();

            System.out.print("Enter the second number: ");
            double b = sc.nextDouble();

            double result = Double.min(a, b);

            System.out.println("The minimum value is " + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Compiling and running the example program

Save the changes to your DoubleMinMethod.java file and compile it using the following command:

javac DoubleMinMethod.java

If there are no errors in your code, the compiler will generate a .class file. You can run the program by typing the following command in the terminal:

java DoubleMinMethod

You should see the program prompt you to enter two numbers. Once you have entered the numbers, the program will display the minimum value between them in the terminal.

Summary

In this lab, you learned how to use the Double.min() method in Java to find the smallest of two double values passed as parameters. You also learned how to create a Java class, import a package, define a main() method, take user input, and display output in the terminal.

Other Java Tutorials you may like