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.

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