Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is the OptionalInt.orElse 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 OptionalInt object is a container object that may or may not contain an integer value.

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

The orElse method

The orElse method will return the integer value present in the OptionalInt object. If the value is not present, then the passed argument is returned.

Syntax

public int orElse(int other)

Parameters

The input parameter is the integer value to be returned if the OptionalInt object is emptyThere is no value present in the OptionalInt object.

Return value

If the OptionalInt object contains an int value, then the value is returned. Otherwise, the passed argument is returned.

Code

The code below shows how to use the orElse method.

import java.util.OptionalInt;
class OptionalIntOrElseExample {
public static void main(String[] args) {
OptionalInt optional1 = OptionalInt.of(1);
System.out.println("Optional1 : " + optional1);
System.out.println("Integer Value at Optional1 is : " + optional1.orElse(10));
OptionalInt optional2 = OptionalInt.empty();
System.out.println("\nOptional2 : " + optional2);
System.out.println("Integer value at Optional2 is : " + optional2.orElse(100));
}
}

Explanation

In the code above:

  • In line 1, we imported the OptionalInt class.
import java.util.OptionalInt;
  • In line 5, we created an OptionalInt object with the integer value 1 using the of method.
OptionalInt optional1 = OptionalInt.of(1);
  • In line 7, we called the orElse method on the optional1 object with 10 as an argument. This method returns 1 because the optional1 object contains the value.
optional1.orElse(10); // 1 
  • In line 9, we used the empty method to get an empty OptionalInt object. The returned object doesn’t have any value.
OptionalInt optional2 = OptionalInt.empty();
  • In line 11, we called the orElse method on the optional2 object with 100 as an argument. This method returns 100 because the optional2 object does not contain any value.
optional2.orElse(100); //100

RELATED TAGS

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