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.


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/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/math("`Math`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/math_methods("`Math Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117623{{"`Java Double Max Method`"}} java/classes_objects -.-> lab-117623{{"`Java Double Max Method`"}} java/class_methods -.-> lab-117623{{"`Java Double Max Method`"}} java/exceptions -.-> lab-117623{{"`Java Double Max Method`"}} java/modifiers -.-> lab-117623{{"`Java Double Max Method`"}} java/oop -.-> lab-117623{{"`Java Double Max Method`"}} java/packages_api -.-> lab-117623{{"`Java Double Max Method`"}} java/user_input -.-> lab-117623{{"`Java Double Max Method`"}} java/wrapper_classes -.-> lab-117623{{"`Java Double Max Method`"}} java/identifier -.-> lab-117623{{"`Java Double Max Method`"}} java/arrays -.-> lab-117623{{"`Java Double Max Method`"}} java/data_types -.-> lab-117623{{"`Java Double Max Method`"}} java/math -.-> lab-117623{{"`Java Double Max Method`"}} java/operators -.-> lab-117623{{"`Java Double Max Method`"}} java/output -.-> lab-117623{{"`Java Double Max Method`"}} java/strings -.-> lab-117623{{"`Java Double Max Method`"}} java/variables -.-> lab-117623{{"`Java Double Max Method`"}} java/math_methods -.-> lab-117623{{"`Java Double Max Method`"}} java/system_methods -.-> lab-117623{{"`Java Double Max Method`"}} end

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