Java Long Max Method

JavaJavaBeginner
Practice Now

Introduction

The Long.max() method is used to return the numerically greater value (maximum value) of the two long numbers passed as arguments. It returns the positive value if one positive and one negative number is passed, and the value with lower magnitude if both the numbers passed are negative.


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/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-117892{{"`Java Long Max Method`"}} java/classes_objects -.-> lab-117892{{"`Java Long Max Method`"}} java/class_methods -.-> lab-117892{{"`Java Long Max Method`"}} java/exceptions -.-> lab-117892{{"`Java Long Max Method`"}} java/modifiers -.-> lab-117892{{"`Java Long Max Method`"}} java/oop -.-> lab-117892{{"`Java Long Max Method`"}} java/packages_api -.-> lab-117892{{"`Java Long Max Method`"}} java/user_input -.-> lab-117892{{"`Java Long Max Method`"}} java/wrapper_classes -.-> lab-117892{{"`Java Long Max Method`"}} java/identifier -.-> lab-117892{{"`Java Long Max Method`"}} java/arrays -.-> lab-117892{{"`Java Long Max Method`"}} java/data_types -.-> lab-117892{{"`Java Long Max Method`"}} java/operators -.-> lab-117892{{"`Java Long Max Method`"}} java/output -.-> lab-117892{{"`Java Long Max Method`"}} java/strings -.-> lab-117892{{"`Java Long Max Method`"}} java/variables -.-> lab-117892{{"`Java Long Max Method`"}} java/system_methods -.-> lab-117892{{"`Java Long Max Method`"}} end

Create a Java file

We will create a Java file to write the code for this example. Open the terminal in Ubuntu and navigate to the project directory by running the following command:

cd ~/project/

Now, create a file named LongMaxExample.java by running below command:

touch LongMaxExample.java

Write the code

In the LongMaxExample.java file, write the below code:

public class LongMaxExample {
    public static void main(String[] args) {
        long a = 5485;
        long b = -3242;
        long c = -5645;

        long max1 = Long.max(a, b);
        long max2 = Long.max(b, c);

        System.out.println("The maximum of " + a + " and " + b + " is " + max1);
        System.out.println("The maximum of " + b + " and " + c + " is " + max2);
    }
}

Save and Compile the code

After writing the code, save the file and close the editor.

Compile the Java file using the below command:

javac LongMaxExample.java

Run the code

After successful compilation, you can run the code using the below command:

java LongMaxExample

This will give the following output:

The maximum of 5485 and -3242 is 5485
The maximum of -3242 and -5645 is -3242

User Input Demonstration

Write the following code to demonstrate how Long.max() can be used with user input:

import java.util.Scanner;

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

            System.out.println("Enter the first number:");
            long num1 = sc.nextLong();
            System.out.println("Enter the second number:");
            long num2 = sc.nextLong();

            System.out.println("The maximum of " + num1 + " and " + num2 + " is " + Long.max(num1, num2));
        }
        catch(Exception e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}

Save and Compile the code

After writing the code, save the file and close the editor.

Compile the Java file using the below command:

javac LongMaxExample.java

Run the code

After successful compilation, you can run the code using the below command:

java LongMaxExample

This will give the following output:

Enter the first number:
-5
Enter the second number:
8
The maximum of -5 and 8 is 8

Enter two long values and the program will output the maximum value of the two numbers.

Summary

Congratulation! You have learned the concepts and implementation of Java Long.max() method. This method helps in finding the maximum value of two long numbers. We have also demonstrated how to use this method in a program with user inputs.

Other Java Tutorials you may like