Signup/Sign In

Java Long getLong(String nm) Method

Java getLong(String nm) method is a part of the Long class of the java.lang package. This method is used to determine the long value that is associated with the system property passed as the string argument. In case, there is no system property attached with the passed string then null is returned.

Syntax:

public static Long getLong(String nm)

Parameter:

The parameter passed is a String nm with which a system property is associated and the long value of the same system property is to be determined.

Returns:

The long value of the system property associated with the passed string argument.

Example 1:

Here, String s1 "sun.arch.data.model" is a system property that determines the version of JVM used. String s2 "StudyTonight" has no system property associated with it, hence null is returned.

We can access the System Properties through the System.getProperty(java.lang.String) method.

However, we can also create our own System property using the System.setProperty(String s, long n) as shown below:

import java.lang.Long;

public class StudyTonight 
{  
    public static void main(String[] args)
    {                    
        String s1 = "sun.arch.data.model";  
        System.out.println("Long Value = "+Long.getLong(s1));  // It determines the long value of the system property       
          
        String s2 = "StudyTonight"; //will return null because no system property is attached with this string 
        System.out.println("Long Value = "+Long.getLong(s2));  
          
        System.setProperty("st.java", "23");  //set a custom property  
        System.out.println("Long Value = " +Long.getLong("st.java"));  
    }  
}


Long Value = 64
Long Value = null
Long Value = 23

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.print("Enter the string value: ");  
            Scanner sc = new Scanner(System.in);  
            String s= sc.next();         
            System.out.println("Long Value = "+Long.getLong(s));  //determines the long value of the system property    
          
        }
        catch(Exception e)
        {
            System.out.println("Invalid Input!!");
        }
    }     
}


Enter the string value: sun.arch.data.model
Long Value = 64
****************************************************
Enter the string value: studytonight
Long Value = null

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.