Java Long getLong() Method

The Long.getLong() method in Java is used to retrieve the long value of a system property. This method can return a default value if the specified property does not exist or cannot be parsed as a long.

Table of Contents

  1. Introduction
  2. getLong() Method Syntax
  3. Examples
    • Retrieving an Existing System Property
    • Retrieving a Non-Existing System Property with Default Value
    • Handling Non-Long Property Values
  4. Real-World Use Case
  5. Conclusion

Introduction

The Long.getLong() method is a static method in the Long class in Java. It retrieves the long value of a system property. If the system property is not defined or cannot be parsed as a long, the method can return null or a specified default value.

getLong()() Method Syntax

The Long.getLong() method has three overloaded versions:

1. Retrieve a Long Property

public static Long getLong(String nm)
  • nm: The name of the system property.

The method returns:

  • The long value of the system property, or null if the property does not exist or cannot be parsed as a long.

2. Retrieve a Long Property with Default Value

public static Long getLong(String nm, long val)
  • nm: The name of the system property.
  • val: The default value to return if the property does not exist or cannot be parsed as a long.

The method returns:

  • The long value of the system property, or the specified default value if the property does not exist or cannot be parsed as a long.

3. Retrieve a Long Property with Default Value (Long)

public static Long getLong(String nm, Long val)
  • nm: The name of the system property.
  • val: The default Long value to return if the property does not exist or cannot be parsed as a long.

The method returns:

  • The long value of the system property, or the specified default Long value if the property does not exist or cannot be parsed as a long.

Examples

Retrieving an Existing System Property

You can retrieve an existing system property and parse it as a long.

Example

public class GetLongExample {
    public static void main(String[] args) {
        // Set a system property for demonstration
        System.setProperty("example.property", "123456789");

        // Retrieve the system property as a long
        Long value = Long.getLong("example.property");

        System.out.println("Long value of 'example.property': " + value);
    }
}

Output:

Long value of 'example.property': 123456789

In this example, the system property example.property is set to "123456789", and the method retrieves it as a long.

Retrieving a Non-Existing System Property with Default Value

You can retrieve a non-existing system property and provide a default value to return if the property does not exist.

Example

public class GetLongWithDefaultExample {
    public static void main(String[] args) {
        // Retrieve the system property as a long with a default value
        Long value = Long.getLong("non.existing.property", 987654321L);

        System.out.println("Long value of 'non.existing.property': " + value);
    }
}

Output:

Long value of 'non.existing.property': 987654321

In this example, the system property non.existing.property does not exist, so the method returns the default value 987654321L.

Handling Non-Long Property Values

If the system property cannot be parsed as a long, the method returns null or the specified default value.

Example

public class GetLongInvalidPropertyExample {
    public static void main(String[] args) {
        // Set a system property with a non-long value for demonstration
        System.setProperty("invalid.property", "abc");

        // Retrieve the system property as a long with a default value
        Long value = Long.getLong("invalid.property", 12345L);

        System.out.println("Long value of 'invalid.property': " + value);
    }
}

Output:

Long value of 'invalid.property': 12345

In this example, the system property invalid.property is set to "abc", which cannot be parsed as a long, so the method returns the default value 12345L.

Real-World Use Case

Configuring Application Settings

In a real-world application, you might use the Long.getLong() method to configure application settings based on system properties.

Example

public class AppConfigExample {
    public static void main(String[] args) {
        // Set system properties for demonstration
        System.setProperty("app.maxConnections", "200");
        System.setProperty("app.timeout", "5000");

        // Retrieve the settings as long values with default values
        Long maxConnections = Long.getLong("app.maxConnections", 100L);
        Long timeout = Long.getLong("app.timeout", 3000L);

        System.out.println("Max Connections: " + maxConnections);
        System.out.println("Timeout: " + timeout);
    }
}

Output:

Max Connections: 200
Timeout: 5000

In this example, the system properties app.maxConnections and app.timeout are set and retrieved as long values with default values.

Conclusion

The Long.getLong() method in Java is a powerful and useful tool for retrieving long values from system properties. By understanding how to use this method, you can efficiently handle tasks that involve parsing and retrieving configuration values in your Java applications. Whether you are dealing with existing properties, providing default values, or handling non-long values, the getLong() method provides a reliable solution for these tasks.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top