Home » Java programming language

Java Math Class static long max(long l1,long l2) with example

Java Math Class static long max(long l1,long l2) method: Here, we are going to learn about the static long max(long l1,long l2) method of Math Class with its syntax and example.
Submitted by Preeti Jain, on September 05, 2019

Math Class static long max(long l1,long l2)

  • This method is available in java.lang package.
  • This method is used to return the maximum one of both the given arguments in the method or in other words it returns the largest value of the given two arguments.
  • This is a static method so it is accessible with the class name too.
  • The return type of this method is long, it returns the largest element from the given two arguments (which are of long types).
  • In this method, we pass two long values (long) parameters as an argument.
  • This method does not throw any exception.

Syntax:

    public static long max(long l1, long l2){
    }

Parameter(s): long l1, long l2 – two long values, in which we have to find the largest value.

Return value:

The return type of this method is long, it returns the largest/maximum value.

Note:

  • If we pass "NaN", it returns "NaN".
  • If we pass the same value as both parameters, it returns the same value.

Java program to demonstrate example of max(long l1, long l2) method

// Java program to demonstrate the example of 
// max(long l1, long l2) method of Math Class

public class MaxLongTypeMethod {
    public static void main(String[] args) {
        // declaring variables
        long l1 = -0l;
        long l2 = 0l;
        long l3 = -2468l;
        long l4 = 12458l;

        // displaying values
        System.out.println("l1: " + l1);
        System.out.println("l2: " + l2);
        System.out.println("l3: " + l3);
        System.out.println("l4: " + l4);

        // Here , we will get (0) because we are 
        // passing parameter whose value is (-0l,0l)
        System.out.println("Math.max(l1,l2): " + Math.max(l1, l2));

        // Here , we will get (12458) and we are 
        // passing parameter whose value is (0l,12458l)
        System.out.println("Math.max(l2,l4): " + Math.max(l2, l4));
    }
}

Output

E:\Programs>javac MaxLongTypeMethod.java

E:\Programs>java MaxLongTypeMethod
l1: 0
l2: 0
l3: -2468
l4: 12458
Math.max(l1,l2): 0
Math.max(l2,l4): 12458


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.