java.lang.Long getLong(String nm,long val)

Description

The Long.getLong(String nm,long val) java method determines the long value of the system property with the specified name.
The first argument is treated as the name of a system property. System properties are accessible through the System.getProperty(java.lang.String) method. The string value of this property is then interpreted as a long value using the grammar supported by decode and a Long object representing this value is returned.

The second argument is the default value. A Long object that represents the value of the second argument is returned if there is no property of the specified name, if the property does not have the correct numeric format, or if the specified name is empty or null.

In other words, this method returns a Long object equal to the value of:

getLong(nm, new Long(val))

but in practice it may be implemented in a manner such as:

Long result = getLong(nm, null);
return (result == null) ? new Long(val) : result;

to avoid the unnecessary allocation of a Long object when the default value is not needed.

Make a note that the getLong() method of Long class is static thus it should be accessed statically which means the we would be calling this method in this format:

Long.getLong(method args)

Non static method is usually called by just declaring method_name(argument) however in this case since the method is static, it should be called by appending the class name as suffix. We will be encountering a compilation problem if we call the java getLong method non statically.

Method Syntax

public static Long getLong(String nm,long val)

Method Argument

Data Type Parameter Description
String nm property name.
long val default value.

Method Returns

The getLong(String nm,long val) method of Long class returns the Long value of the property.

Compatibility

Requires Java 1.0 and up

Java Long getLong(String nm,long val) Example

Below is a simple java example on the usage of getLong(String nm,long val) method of Long class.

package com.javatutorialhq.java.examples;

import java.util.Properties;

/*
 * This example source code demonstrates the use of 
 * getLong(String nm, long val) method of Boolean class.
 */

public class LongGetLongValueExample {

	public static void main(String[] args) {
		
		// call a method to set a property
		setProperty();
		// check if the property is existing
		// and get the value. If it is not available
		// default value will be returned
		Long testLong = Long.getLong("test.long",1);		
		
		// print the result
		System.out.println("Result:"+testLong);
		
	}
	/*
	 * This is a method to set a property
	 * to test out the getLong() method
	 */
	public static void setProperty(){
		// instantiate a new Properties
		// equal to system properties
		Properties prop = System.getProperties();
		
		// set a property with key as test.long
		// and value is "Test String"
		prop.setProperty("test.long", "123");
	}

}

Sample Output

Below is the sample output when you run the above example.

Java Long getLong(String nm,long val) example output