Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

optionallong
empty
java
communitycreator

What is the OptionalLong.empty method in Java?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

In Java, the OptionalLong object is a container object which may or may not contain a long value.

The OptionalLong class is present in the java.util package.

The empty() method

The empty() method is used to get the empty instance of the OptionalLong class. The returned object doesn’t have any value.

Syntax


public static OptionalLong empty()

Arguments

This method doesn’t take an argument.

Return value

This method returns an OptionalLong object with an empty value.

Code

The code below denotes how to use the empty method.

import java.util.OptionalLong;
class OptionalLongEmptyExample {
public static void main(String[] args) {
OptionalLong optional = OptionalLong.empty();
// print value
System.out.println("OptionalLong : " + optional);
System.out.println("Has value: " + optional.isPresent());
}
}

Explanation

In the code above:

  • Line 1: We imported the OptionalLong class.


    import java.util.OptionalLong;
    

  • Line 5: We used the empty method to get an empty OptionalLong object. The returned object doesn’t have any value.


    OptionalLong optional = OptionalLong.empty();
    

  • Line 7: We printed the created OptionalLong object.


    System.out.println("OptionalLong: " + optional); // OptionalLong.empty
    

  • Line 8: We used the isPresent method to check if the object contains a value. We will get false as a result.


    optional.isPresent(); // false
    

RELATED TAGS

optionallong
empty
java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring