Signup/Sign In

Java Long min() Method

Java min() method is a part of the Long class of the java.lang package and is specified by the Math.min() method of the Math class. This method is used to return the numerically smaller value(minimum value) of the two numbers(long) passed as arguments.

If a positive and a negative number is passed then the negative value is returned but if both the numbers passed are negative then a higher magnitude value is returned.

Syntax:

public static long min(long a, long b)

Parameters:

The parameters passed are the long values by the user to check for the minimum of the two numbers passed.

Returns:

The numerically lower value out of the two long values passed as the parameter.

Example 1:

Here, one positive and one negative number is taken, hence the negative value is returned and in the case of both negative, the value with higher magnitude is returned.

import java.lang.Long;

public class StudyTonight 
{  
    public static void main(String[] args) 
    {      
          long x = 5485L;  
          long y = -3242L; 
          long z = -5645L;
    
          System.out.println("Lower number is " + Long.min(x, y));  // print the smaller number between x and y 
          System.out.println("Lower number is " + Long.min(y, z));  // print the smaller number between y and z
    }  
}


Lower number is -3242
Lower number is -5645

Example 2:

Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent output.

import java.util.Scanner; 

public class StudyTonight
{  
    public static void main(String[] args) 
    {  
        try
        {
           System.out.println("Enter the Two values: ");  
           Scanner sc= new Scanner(System.in);  
           long a = sc.nextLong();  
           long b = sc.nextLong();  
                       
           System.out.println("Smaller value is " + Long.min(a, b)); //Print the smaller number between a and b 
          
        }
        catch(Exception e)
        {
          
        } 
    }  
}  


Enter the Two values:
856 786
Smaller value is 786
****************************
Enter the Two values:
-675 -434
Smaller value is -675

Live Example:

Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.



About the author:
A Computer Science and Engineering Graduate(2016-2020) from JSSATE Noida. JAVA is Love. Sincerely Followed Sachin Tendulkar as a child, M S Dhoni as a teenager, and Virat Kohli as an adult.