Right.

public static Optional<String> getFileMd5(String filePath)
        throws NoSuchAlgorithmException, IOException {

        return Optional.empty(); // I.o. null

    return Optional.of(nonnullString);
}

Usage:

getFileMd5(filePath).ifPresent((s) -> { ... });

or (less nice as undoing the Optional)

String s = getFileMd5(filePath).orElse("" /* null */);
Answer from Joop Eggen on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Optional.html
Optional (Java Platform SE 8 )
October 20, 2025 - Optional<FileInputStream> fis = names.stream().filter(name -> !isProcessedYet(name)) .findFirst() .map(name -> new FileInputStream(name)); Here, findFirst returns an Optional<String>, and then map returns an Optional<FileInputStream> for the desired file if one exists. ... an Optional describing the result of applying a mapping function to the value of this Optional, if a value is present, otherwise an empty Optional
🌐
Baeldung
baeldung.com › home › java › core java › guide to java optional
Guide To Java Optional | Baeldung
December 8, 2016 - So, there are solutions to avoid using Optionals as method parameters. The intent of Java when releasing Optional was to use it as a return type, thus indicating that a method could return an empty value.
🌐
Baeldung
baeldung.com › home › java › java optional as return type
Java Optional as Return Type | Baeldung
January 8, 2024 - The Optional type was introduced in Java 8. It provides a clear and explicit way to convey the message that there may not be a value, without using null. When getting an Optional return type, we’re likely to check if the value is missing, ...
🌐
DZone
dzone.com › coding › java › considerations when returning java 8's optional from a method
Considerations When Returning Java 8's Optional From a Method
January 31, 2018 - NEVER have that method return null [Optional.empty() should generally be returned instead]. I'm going to reiterate this point with two quotations: Highlighted sentence from Item #55 in Effective Java, Third Edition: "Never return a null value from an Optional-returning method."
🌐
Reddit
reddit.com › r/javahelp › what is better practice when returning an optional of an object, but the object is not found
r/javahelp on Reddit: What is better practice when returning an Optional of an object, but the object Is not found
February 8, 2022 -

My service layer has a method which return an Optional of an object by the id.

My question is what the better practice, when the object is not found

· Throw an exception.

· Return empty Optional.

I tend toward returning an empty Optional since I see no reason to use Optional in the first place if I intend the method to throw exception.

Anyway I would like to hear your opinion on the subject.

🌐
DZone
dzone.com › coding › java › how to use optionals in java
How to Use Optionals in Java
June 16, 2020 - Instead, we need a mechanism that allows a method developer to explicitly denote that the return value for a method may or may not be present. This mechanism was introduced in Java Development Kit (JDK) 7 in the form of the Optional class.
Find elsewhere
🌐
Javaprogramto
javaprogramto.com › 2020 › 09 › java-optional-as-return-type.html
Java Optional as Return Type JavaProgramTo.com
When you are expecting Optional as return type then the returned object may have the value or null value. In both cases, you should use the right methods of Optional such as ofNullable(), ifPresent() and get(). package com.javaprogramto.jav...
🌐
Developer.com
developer.com › dzone › coding › java › how to use optionals in java
How to use Optional in Java
June 16, 2020 - Instead, we need a mechanism that allows a method developer to explicitly denote that the return value for a method may or may not be present. This mechanism was introduced in Java Development Kit (JDK) 7 in the form of the Optional class.
🌐
Medium
medium.com › geekculture › effective-java-return-optionals-judiciously-719b28110cb0
Effective Java! Return Optionals Judiciously | by Kyle Carter | Geek Culture | Medium
June 21, 2021 - Prior to Java 8 if a method didn’t want to return a value some of the time and return a value at other times there were a few options. The method could return a null, the method could throw an exception, or you could come up with some state holding object that could be returned (although I've never seen this).
🌐
Reddit
reddit.com › r/java › is it still considered good practice to return optional for getters?
r/java on Reddit: Is it still considered good practice to return Optional for getters?
March 6, 2016 -

I've been reading about this topic for a while, and there seems to be different trains of thought.

If you watch the presentation by Goetz and Stuart Marks about the Java 8 features, it seems that they are against Optional being used this way.
Goetz even makes a comment about it here: https://stackoverflow.com/a/26328555

However, if you look at other stackoverflow answers or even reddit threads (like https://www.reddit.com/r/java/comments/6h91ms/method_return_optional_or_null/), you'll notice that a lot of people do support return Optional for a getter.

So now that this Optional API has been in place for a while, what are the recommendations for it? How have you been using it?

🌐
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 - If a value is present, returns the value, otherwise throws an exception produced by the exception supplying function. ... A method reference to the exception constructor with an empty argument list can be used as the supplier. For example, IllegalStateException::new ...
🌐
Indrek
blog.indrek.io › articles › misusing-java-optional
Misusing Java’s Optional type | That Which Inspires Awe
October 5, 2019 - Optionals were introduced to Java in version 8. Prior to it, when we had no value to return from a method, we could for example return a null or throw an exception. Both options have their drawbacks. If the calling code forgets to check the return value for null, there’s a high chance for ...
🌐
Dariawan
dariawan.com › tutorials › java › using-java-optional-correctly
Using Java Optional Correctly | Dariawan
The Java language authors have been quite frank that Optional was intended for use only as a return type, as a way to convey that a method may or may not return a value.
🌐
Tom Gregory
tomgregory.com › java-optional
Optional in Java: Everything You Need To Know | Tom Gregory
July 17, 2022 - The great thing about calling a method that returns an Optional is that we immediately know that the object can be in one of 2 states: ... All this is done in a very safe way, avoiding any chance of NullPointerException. And it's a lot less verbose than if we were using null values.
🌐
DZone
dzone.com › coding › languages › 26 reasons why using optional correctly is not optional
26 Reasons Why Using Optional Correctly Is Not Optional
November 28, 2018 - Usingfilter()to accept or reject a wrapped value is a very convenient approach since it can be accomplished without explicitly unwrapping the value. ... // AVOID public boolean validatePasswordLength(User userId) { Optional<String> password = ...; // User password if (password.isPresent()) { return password.get().length() > 5; } return false; } ... // PREFER public boolean validatePasswordLength(User userId) { Optional<String> password = ...; // User password return password.filter((p) -> p.length() > 5).isPresent(); } If so, we then use the Optional.stream() method. Starting with Java 9, we can treat theOptionalinstance as aStreamby applyingOptional.stream()method.
🌐
CalliCoder
callicoder.com › java-8-optional-tutorial
Java Optional Tutorial with Examples | CalliCoder
February 18, 2022 - The filter() method takes a predicate as an argument. If the Optional contains a non-null value and the value matches the given predicate, then filter() method returns an Optional with that value, otherwise it returns an empty Optional.
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › util › Optional.html
Optional (Java SE 17 & JDK 17)
October 20, 2025 - This is a value-based class; ... synchronization may fail. ... Optional is primarily intended for use as a method return type where there is a clear need to represent "no result," and where using null is likely to cause errors....