Home » Java programming language

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

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

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

  • This method is available in java.lang package.
  • This method is used to return the minimum one of both the given arguments or in other words this method returns the smallest value of the given two arguments.
  • This is a static method so this method is accessible with the class name too.
  • The return type of this method is long, it returns the smallest element from the given two arguments.
  • This method accepts two arguments of long values.
  • This method does not throw any exception.

Syntax:

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

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

Return value:

The return type of this method is long, it returns the smallest/minimum value.

Note:

  • If we pass "NaN" (Not a Number), it returns the same value i.e. "NaN".
  • If we pass zero (-0 or 0), it returns the 0.
  • If we pass the same values in both parameters, it returns the same value.

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

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

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

        // displaying the 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.min(l1,l2): " + Math.min(l1, l2));

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

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

Output

E:\Programs>javac MinLongTypeMethod.java

E:\Programs>java MinLongTypeMethod
l1: 0
l2: 0
l3: -2
l4: 12458
Math.min(l1,l2): 0
Math.min(l1,l3): -2
Math.min(l1,l2): 0



Comments and Discussions!

Load comments ↻






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