Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
long
communitycreator

What is the OptionalLong.of 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.

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.

What is the of method of the OptionalLong class?

The of method is used to get an instance of the OptionalLong class with the specified long value.

Syntax

public static OptionalLong of(long value)

Argument

This function takes in the long value which is to be present in the OptionalLong object.

Return value

This method returns an OptionalLong object with the specified long value.

Code

The code below denotes how to use the of method.

import java.util.OptionalLong;
class OptionalLongOfExample {
public static void main(String[] args) {
OptionalLong optional1 = OptionalLong.of(1);
System.out.println("Optional 1: " + optional1);
OptionalLong optional2 = OptionalLong.of(2147483648l);
System.out.println("Optional 2: " + optional2);
}
}

Explanation

In the code above:

  • In line number 1, we imported the OptionalLong class.
import java.util.OptionalLong;
  • In line number 5, we used the of method to get an OptionalLong object with the long value 1.
OptionalLong optional1 = OptionalLong.of(1);
optional1; // OptionalLong[1]
  • In line number 8, we used the of method to get an OptionalLong object with the value 2147483648l.
OptionalLong optional2 = OptionalLong.of(2147483648l);
optional2;// OptionalLong[2147483648]

RELATED TAGS

java
long
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