Signup/Sign In

Java Long getLong(String nm, Long val) Method

Java getLong(String, Long) method of the Long class is also used to determine the long value that is associated with the system property passed as the string argument. The second argument here is also the default but rather than the default value a default Long object is returned.

Syntax:

public static Long getLong(String nm, Long val)

Parameters:

The parameters passed are String nm whose system property's long value is to be determined and Long val which is the default Long object which will be returned if no system property is found.

Returns:

The long value associated with the system property of the passed string or default Long object if there is no system property associated with the string.

Example 1:

Here, for string s the long value of the system property is returned and the default Long object is returned for the string which doesn't have a system property value or name.

import java.lang.Long;

public class StudyTonight 
{  
    public static void main(String[] args)
    {          
        System.out.println(Long.getLong("java.vm.specification.vendor", 25));// Print default system property   
        
        System.out.println("Default value is:"+ Long.getLong("sun.arch.data.model")); //Prints the system property 
       
        // setting and printing the custom property  
        System.setProperty("studytonight.java", "80");  
        
        System.out.println("Custom Property: " + Long.getLong("studytonight.java"));  
        
        //printing a custom property which is not set will yield default value
        System.out.println("Custom Property: " + Long.getLong("mohit", 90));  
  
    }  
}


25
Default value is:64
Custom Property: 80
Custom Property: 90

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 value and default value ");                   
            Scanner sc = new Scanner(System.in);  
            String s = sc.next();
            Long i= sc.nextLong(); //Long object for default value
            System.out.println("Value is : "+Long.getLong(s, i)); //will returns the long value of the system property 
        }
        catch(Exception e)
        {
            System.out.println("Exception occurred" + e);
        }
    }  
}  


Enter the value and default value
mohit 77
Value is : 77
******************************************
Enter the value and default value
sun.arch.data.model 23
Value is : 64

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.