Java Optional and orElse

The orElse stream is never invoked but the method itself is executed. This means that the method parameter is passed to it as well. So the part Optional.ofNullable(t).orElseThrow(() -> new Exception("MyException")) is being called regardless of the value passed to the first Optional.ofNullable call.

If you don't want this to happen you will need to pass a Supplier like this:

String t = null;
Optional.ofNullable("notnull")
    .orElseGet(
        () -> Optional.ofNullable(t).orElseThrow(() -> new RuntimeException("MyException"))
    );

The supplier is only invoked when the orElseGet stream is invoked. Note that you'll need a RuntimeException instead of a checked exception in order to be able to break from the supplier.


That's because the code inside orElse() will be always evaluated. In other words it will be executed even if you specify a non-empty Optional, so that's why the Exception will be thrown.

If you check the orElse() section of Java Optional – orElse() vs orElseGet() article, you can see that in their example, where it says:

We can easily infer that the parameter of orElse() is evaluated even when having a non-empty Optional.


What you wrote is the same like this:

String t = null;
String myException = Optional.ofNullable(t).orElseThrow(() -> new Exception("MyException"));
Optional.ofNullable("notnull").orElse(myException);

Part orElse of Optional is evaluated before you know is your value null or not.If you want "Lazy" evaluation consider orElseGet method.

Tags:

Java

Optional