What is the OptionalInt.orElse method in Java?

What is the OptionalInt.orElse method in Java? Answer

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 empty.

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
What is the OptionalInt.orElse method in Java? Review:

In our experience, we suggest you solve this What is the OptionalInt.orElse method in Java? and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the What is the OptionalInt.orElse method in Java?

Find on Educative

Conclusion:

I hope this What is the OptionalInt.orElse method in Java? would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published.