Short Answer:

  • orElse() will always call the given function whether you want it or not, regardless of Optional.isPresent() value
  • orElseGet() will only call the given function when the Optional.isPresent() == false

In real code, you might want to consider the second approach when the required resource is expensive to get.

Copy// Always get heavy resource
getResource(resourceId).orElse(getHeavyResource()); 

// Get heavy resource when required.
getResource(resourceId).orElseGet(() -> getHeavyResource()) 

For more details, consider the following example with this function:

Copypublic Optional<String> findMyPhone(int phoneId)

The difference is as below:

Copy                           X : buyNewExpensivePhone() called

+β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”+β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”+
|           Optional.isPresent()                                   | true | false |
+β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”+β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”+
| findMyPhone(int phoneId).orElse(buyNewExpensivePhone())          |   X  |   X   |
+β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”+β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”+
| findMyPhone(int phoneId).orElseGet(() -> buyNewExpensivePhone()) |      |   X   |
+β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”+β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”+

When optional.isPresent() == false, there is no difference between two ways. However, when optional.isPresent() == true, orElse() always calls the subsequent function whether you want it or not.

Finally, the test case used is as below:

Result:

Copy------------- Scenario 1 - orElse() --------------------
  1.1. Optional.isPresent() == true (Redundant call)
    Going to a very far store to buy a new expensive phone
    Used phone: MyCheapPhone

  1.2. Optional.isPresent() == false
    Going to a very far store to buy a new expensive phone
    Used phone: NewExpensivePhone

------------- Scenario 2 - orElseGet() --------------------
  2.1. Optional.isPresent() == true
    Used phone: MyCheapPhone

  2.2. Optional.isPresent() == false
    Going to a very far store to buy a new expensive phone
    Used phone: NewExpensivePhone

Code:

Copypublic class TestOptional {
    public Optional<String> findMyPhone(int phoneId) {
        return phoneId == 10
                ? Optional.of("MyCheapPhone")
                : Optional.empty();
    }

    public String buyNewExpensivePhone() {
        System.out.println("\tGoing to a very far store to buy a new expensive phone");
        return "NewExpensivePhone";
    }


    public static void main(String[] args) {
        TestOptional test = new TestOptional();
        String phone;
        System.out.println("------------- Scenario 1 - orElse() --------------------");
        System.out.println("  1.1. Optional.isPresent() == true (Redundant call)");
        phone = test.findMyPhone(10).orElse(test.buyNewExpensivePhone());
        System.out.println("\tUsed phone: " + phone + "\n");

        System.out.println("  1.2. Optional.isPresent() == false");
        phone = test.findMyPhone(-1).orElse(test.buyNewExpensivePhone());
        System.out.println("\tUsed phone: " + phone + "\n");

        System.out.println("------------- Scenario 2 - orElseGet() --------------------");
        System.out.println("  2.1. Optional.isPresent() == true");
        // Can be written as test::buyNewExpensivePhone
        phone = test.findMyPhone(10).orElseGet(() -> test.buyNewExpensivePhone());
        System.out.println("\tUsed phone: " + phone + "\n");

        System.out.println("  2.2. Optional.isPresent() == false");
        phone = test.findMyPhone(-1).orElseGet(() -> test.buyNewExpensivePhone());
        System.out.println("\tUsed phone: " + phone + "\n");
    }
}
Answer from Hoa Nguyen on Stack Overflow
🌐
Baeldung
baeldung.com β€Ί home β€Ί java β€Ί core java β€Ί java optional – orelse() vs orelseget()
Java Optional - orElse() vs orElseGet() | Baeldung
January 16, 2024 - orElseGet(): returns the value if present, otherwise invokes other and returns the result of its invocation Β· It’s easy to be a bit confused by these simplified definitions, so let’s dig a little deeper and look at some actual usage scenarios. Assuming we have our logger configured properly, let’s start with writing a simple piece of code: String name = Optional.of("baeldung") .orElse(getRandomName());
🌐
Oracle
docs.oracle.com β€Ί javase β€Ί 8 β€Ί docs β€Ί api β€Ί java β€Ί util β€Ί Optional.html
Optional (Java Platform SE 8 )
April 21, 2026 - If a value is present, isPresent() will return true and get() will return the value. Additional methods that depend on the presence or absence of a contained value are provided, such as orElse() (return a default value if value not present) and ifPresent() (execute a block of code if the value ...
Top answer
1 of 10
287

Short Answer:

  • orElse() will always call the given function whether you want it or not, regardless of Optional.isPresent() value
  • orElseGet() will only call the given function when the Optional.isPresent() == false

In real code, you might want to consider the second approach when the required resource is expensive to get.

Copy// Always get heavy resource
getResource(resourceId).orElse(getHeavyResource()); 

// Get heavy resource when required.
getResource(resourceId).orElseGet(() -> getHeavyResource()) 

For more details, consider the following example with this function:

Copypublic Optional<String> findMyPhone(int phoneId)

The difference is as below:

Copy                           X : buyNewExpensivePhone() called

+β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”+β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”+
|           Optional.isPresent()                                   | true | false |
+β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”+β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”+
| findMyPhone(int phoneId).orElse(buyNewExpensivePhone())          |   X  |   X   |
+β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”+β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”+
| findMyPhone(int phoneId).orElseGet(() -> buyNewExpensivePhone()) |      |   X   |
+β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”+β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”+

When optional.isPresent() == false, there is no difference between two ways. However, when optional.isPresent() == true, orElse() always calls the subsequent function whether you want it or not.

Finally, the test case used is as below:

Result:

Copy------------- Scenario 1 - orElse() --------------------
  1.1. Optional.isPresent() == true (Redundant call)
    Going to a very far store to buy a new expensive phone
    Used phone: MyCheapPhone

  1.2. Optional.isPresent() == false
    Going to a very far store to buy a new expensive phone
    Used phone: NewExpensivePhone

------------- Scenario 2 - orElseGet() --------------------
  2.1. Optional.isPresent() == true
    Used phone: MyCheapPhone

  2.2. Optional.isPresent() == false
    Going to a very far store to buy a new expensive phone
    Used phone: NewExpensivePhone

Code:

Copypublic class TestOptional {
    public Optional<String> findMyPhone(int phoneId) {
        return phoneId == 10
                ? Optional.of("MyCheapPhone")
                : Optional.empty();
    }

    public String buyNewExpensivePhone() {
        System.out.println("\tGoing to a very far store to buy a new expensive phone");
        return "NewExpensivePhone";
    }


    public static void main(String[] args) {
        TestOptional test = new TestOptional();
        String phone;
        System.out.println("------------- Scenario 1 - orElse() --------------------");
        System.out.println("  1.1. Optional.isPresent() == true (Redundant call)");
        phone = test.findMyPhone(10).orElse(test.buyNewExpensivePhone());
        System.out.println("\tUsed phone: " + phone + "\n");

        System.out.println("  1.2. Optional.isPresent() == false");
        phone = test.findMyPhone(-1).orElse(test.buyNewExpensivePhone());
        System.out.println("\tUsed phone: " + phone + "\n");

        System.out.println("------------- Scenario 2 - orElseGet() --------------------");
        System.out.println("  2.1. Optional.isPresent() == true");
        // Can be written as test::buyNewExpensivePhone
        phone = test.findMyPhone(10).orElseGet(() -> test.buyNewExpensivePhone());
        System.out.println("\tUsed phone: " + phone + "\n");

        System.out.println("  2.2. Optional.isPresent() == false");
        phone = test.findMyPhone(-1).orElseGet(() -> test.buyNewExpensivePhone());
        System.out.println("\tUsed phone: " + phone + "\n");
    }
}
2 of 10
242

Take these two scenarios:

CopyOptional<Foo> opt = ...
Foo x = opt.orElse( new Foo() );
Foo y = opt.orElseGet( Foo::new );

If opt doesn't contain a value, the two are indeed equivalent. But if opt does contain a value, how many Foo objects will be created?

P.s.: of course in this example the difference probably wouldn't be measurable, but if you have to obtain your default value from a remote web service for example, or from a database, it suddenly becomes very important.

🌐
Baeldung
baeldung.com β€Ί home β€Ί java β€Ί optional orelse optional
Optional orElse Optional in Java | Baeldung
March 17, 2024 - @Test public void givenTwoOptionalMethods_whenFirstNonEmpty_thenSecondNotEvaluated() { ItemsProvider itemsProvider = new ItemsProvider(); Optional<String> item = itemsProvider.getNail() .map(Optional::of) .orElseGet(itemsProvider::getHammer); assertEquals(Optional.of("nail"), item); } The above test case prints only β€œReturning a nail”. This clearly indicates that only the getNail() method has been executed. Java 9 has added an or() method that we can use to get an Optional, or another value, if that Optional isn’t present.
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί java β€Ί optional-orelse-method-in-java-with-examples
Optional orElse() method in Java with examples - GeeksforGeeks
July 12, 2025 - The orElse() method of java.util.Optional class in Java is used to get the value of this Optional instance, if present. If there is no value present in this Optional instance, then this method returns the specified value.
🌐
Medium
medium.com β€Ί @AlexanderObregon β€Ί javas-optional-orelse-method-explained-658010699536
Java’s Optional.orElse() Method Explained | Medium
September 7, 2024 - It provides a way to represent optional values, essentially values that may or may not be present. One of the most commonly used methods in this class is orElse(), which allows you to return a default value if the Optional is empty.
🌐
Medium
medium.com β€Ί @AlexanderObregon β€Ί javas-optional-orelseget-method-explained-d65c97250a9e
Java’s Optional.orElseGet() Explained | Medium
September 27, 2024 - Even if the Optional is not empty, the expensive computation will still occur. orElseGet() avoids this issue by only computing the default value when absolutely necessary, making it the better choice in scenarios involving expensive operations. Understanding when to use each method can save both time and computational resources in your Java ...
🌐
Baeldung
baeldung.com β€Ί home β€Ί java β€Ί core java β€Ί guide to java optional
Guide To Java Optional | Baeldung
February 15, 2026 - In the above example, we use only ... validation as well as execute the code. The orElse() method is used to retrieve the value wrapped inside an Optional instance....
Find elsewhere
🌐
Educative
educative.io β€Ί answers β€Ί what-is-the-optionalorelse-method-in-java
What is the Optional.orElse() method in Java?
In Java, the Optional object is ... package. You can read more about the Optional class here. The orElse() method will return the value present in an Optional object....
🌐
HowToDoInJava
howtodoinjava.com β€Ί home β€Ί java 8 β€Ί java optional orelse vs. oreleseget (with examples)
Java Optional orElse Vs. OrEleseGet (with Examples)
April 16, 2024 - Rather than checking the presence ... Its two methods orElse() and OrEleseGet() do exactly the same and return a default value if the Optional contains null....
🌐
Oracle
docs.oracle.com β€Ί en β€Ί java β€Ί javase β€Ί 11 β€Ί docs β€Ί api β€Ί java.base β€Ί java β€Ί util β€Ί Optional.html
Optional (Java SE 11 & JDK 11 )
January 20, 2026 - Additional methods that depend on the presence or absence of a contained value are provided, such as orElse() (returns a default value if no value is present) and ifPresent() (performs an action if a value is present). This is a value-based class; use of identity-sensitive operations (including ...
🌐
Medium
medium.com β€Ί @navnathujadhav β€Ί understanding-the-difference-between-orelse-and-orelseget-in-javas-optional-class-ce786b6cde8e
Understanding the Difference Between orElse and orElseGet in Java's Optional Class | by Navnath Jadhav | Medium
May 21, 2023 - In Java, the Optional class offers powerful methods to handle nullable values. Two such methods, orElse and orElseGet, are used to provide a default value when an Optional instance is empty.
🌐
Oracle
docs.oracle.com β€Ί javase β€Ί 9 β€Ί docs β€Ί api β€Ί java β€Ί util β€Ί Optional.html
Optional (Java SE 9 & JDK 9 )
Returns an Optional describing the given value, if non-null, otherwise returns an empty Optional. ... If a value is present, returns the value, otherwise throws NoSuchElementException. ... The methods orElse and orElseGet are generally preferable to this method, as they return a substitute ...
🌐
Oracle
docs.oracle.com β€Ί en β€Ί java β€Ί javase β€Ί 17 β€Ί docs β€Ί api β€Ί java.base β€Ί java β€Ί util β€Ί Optional.html
Optional (Java SE 17 & JDK 17)
April 21, 2026 - Additional methods that depend on the presence or absence of a contained value are provided, such as orElse() (returns a default value if no value is present) and ifPresent() (performs an action if a value is present). This is a value-based class; programmers should treat instances that are ...
🌐
Mrhaki
blog.mrhaki.com β€Ί 2020 β€Ί 12 β€Ί java-joy-optional-orelse-orelseget-that.html
Java Joy: Optional orElse orElseGet That Is The Question - Messages from mrhaki
December 30, 2020 - */ private static int totalDefaultGreetingInvoked = 0; public static void main(String[] args) { // Define an empty optional. var name = Optional.ofNullable(null); // orElse returns value from argument when optional is empty. assert ("Hello " ...
🌐
Java67
java67.com β€Ί 2018 β€Ί 06 β€Ί java-8-optional-example-ispresent-orElse-get.html
Java 8 Optional isPresent(), OrElse() and get() Examples | Java67
You just need to take the literal meaning of Optional that value may or may not be present and you have to code accordingly. This will think you what to do in case of value is not present like using a default value or throwing a proper error message. On a related note, Java is moving really fast and we are already in Java 12, still, a lot of developers have to learn Java 8, particularly the functional programming aspect.
🌐
Microsoft Learn
learn.microsoft.com β€Ί en-us β€Ί dotnet β€Ί api β€Ί java.util.optional.orelse
Optional.OrElse(Object) Method (Java.Util) | Microsoft Learn
May be null. Object Β· the value, if present, otherwise other Β· Attributes Β· RegisterAttribute Β· If a value is present, returns the value, otherwise returns other. Java documentation for java.util.Optional.orElse(T).
🌐
Java Code Geeks
javacodegeeks.com β€Ί home β€Ί core java
Optional orElse vs orElseGet - Java Code Geeks
July 1, 2020 - OrElse method takes a parameter which will be returned if the optional doesn’t have value.
🌐
Oracle
docs.oracle.com β€Ί javase β€Ί 10 β€Ί docs β€Ί api β€Ί java β€Ί util β€Ί Optional.html
Optional (Java SE 10 & JDK 10 )
Stream<Optional<T>> os = .. Stream<T> s = os.flatMap(Optional::stream) ... If a value is present, returns the value, otherwise returns other. ... If a value is present, returns the value, otherwise returns the result produced by the supplying function. ... If a value is present, returns the ...