Java Double Max Method

JavaJavaBeginner
Practice Now

Introduction

Java max() method is a built-in method that is available in the Double class of the java.lang package in Java. This method is used to return the numerically greater value(maximum value) of the two numbers passed as arguments. If you are working with double values and you want to find the maximum value between two provided values, Double.max() method is a convenient way to do that.

Creating a Java File

To start with, navigate to the home directory using the terminal and create a new Java file named MaxDouble.java using the following command:

touch MaxDouble.java

Writing Java Code

Type the following code in the MaxDouble.java file using the code editor to understand how the Double.max() method works.

public class MaxDouble {
    public static void main(String[] args) {
        double x = 34.5;
        double y = -20.1;
        System.out.println("Larger number is " + Double.max(x, y));
    }
}

In the above code, we have created a class named MaxDouble with a main method. Inside the main method, we have declared two variables x and y of double data type and assigned the values 34.5 and -20.1, respectively. We then printed the maximum value between x and y using the Double.max() method.

Compiling the Java Code

Save the changes to the MaxDouble.java file and exit the code editor.

Compile the MaxDouble.java file using the following command:

javac MaxDouble.java

Running the Java Code

Run the compiled Java program using the following command:

java MaxDouble

The output of the executed program is:

Larger number is 34.5

Taking Input from the User

Let's try to take two double values as input from the user and find the maximum value between them using the Double.max() method. Modify the code in the MaxDouble.java file to the following:

import java.util.Scanner;

public class MaxDouble {
    public static void main(String[] args) {
        try{
            Scanner sc = new Scanner(System.in);
            System.out.print("Enter the first number: ");
            double num1 = sc.nextDouble();
            System.out.print("Enter the second number: ");
            double num2 = sc.nextDouble();
            System.out.println("Larger number is " + Double.max(num1, num2));
        }
        catch(Exception e){
            System.out.println("Invalid input!!");
        }

    }
}

In the above code, we have used the Scanner class to take two double values as input from the user. Then we have printed the maximum value between the two input numbers using the Double.max() method.

Compiling the Java Code

Save the changes to the MaxDouble.java file and exit the code editor.

Compile the MaxDouble.java file using the following command:

javac MaxDouble.java

Running the Java Code

Run the compiled Java program using the following command:

java MaxDouble

The output of the executed program is:

Enter the first number: 35.4
Enter the second number: 67.89
Larger number is 67.89

Using Math.max() instead of Double.max()

In Java, we can also use the Math.max() method to find the maximum value between two double values. The Math.max() method is a built-in method in the java.lang package. Let's modify the above code to use the Math.max() method instead of the Double.max() method.

import java.util.Scanner;

public class MaxDouble {
    public static void main(String[] args) {
        try{
            Scanner sc = new Scanner(System.in);
            System.out.print("Enter the first number: ");
            double num1 = sc.nextDouble();
            System.out.print("Enter the second number: ");
            double num2 = sc.nextDouble();
            System.out.println("Larger number is " + Math.max(num1, num2));
        }
        catch(Exception e){
            System.out.println("Invalid input!!");
        }

    }
}

In the above code, we have replaced the Double.max() method with the Math.max() method.

Compiling the Java Code

Save the changes to the MaxDouble.java file and exit the code editor.

Compile the MaxDouble.java file using the following command:

javac MaxDouble.java

Running the Java Code

Run the compiled Java program using the following command:

java MaxDouble

The output of the executed program is:

Enter the first number: 23.98
Enter the second number: 598.6
Larger number is 598.6

Summary

In this lab, we have learned about the Double.max() method in Java and how to use this method to find the maximum value between two double values. We have also seen some examples to understand the usage of Double.max() method. Additionally, we have also learned how to take input from the user and find the maximum value between two numbers using the Double.max() and Math.max() method in Java.

Other Java Tutorials you may like