LAST UPDATED: SEPTEMBER 3, 2020
Java Long getLong(String nm, int val) Method
The Java getLong(String nm, long val)
method of the Long
class is used to determine the long value that is associated with the system property passed as the string argument. The second argument val is the default value and is returned if:
-
no property of the given name exists.
-
if the property is not given the correct format
-
or, the passed property is either null or empty.
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 value 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 value 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 value 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)
{
String s = "sun.arch.data.model";
System.out.println(Long.getLong(s, 0)); //returns the long value of the system property of string s
System.out.println(Long.getLong("java.vm.specification.vendor", 100)); // will return the default value as string as there is no property of the given name
System.out.println(Long.getLong("studytonight",0)); // will return the default value as string does not have a system property value
}
}
64
100
0
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();
System.out.println("Default Value: "+Long.getLong(s, i)); //will returns the long value of the system property
}
catch(Exception e)
{
System.out.println("Exception occured" + e);
}
}
}
Enter the value and default value
studytonight 88
Default Value: 88
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.