Java 8 Optional. Why of and ofNullable?

后端 未结 8 1508
旧时难觅i
旧时难觅i 2021-02-07 01:01

I have a question regarding Java 8\'s Optional, the purpose of which is to tackle NullPointerException exceptions.

The question is, what is the reason for h

相关标签:
8条回答
  • 2021-02-07 01:34

    The javadoc of Optional.of reads that explicitly :

    @throws NullPointerException if value is null
    

    and that is where the requirement of handling the cases as expected by you comes into picture with the use of Optional.ofNullable which is a small block of code as :

    public static <T> Optional<T> ofNullable(T value) {
        return value == null ? empty() : of(value); // 'Optional.of'
    }
    

    That said, the decision of choosing one over the other would still reside with the application design as if your value could possibly be null or not.


    On your expectation part, that was not what the Optional was actually intended for. The API note clarifies this further (formatting mine):

    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 error. A variable whose type is Optional should never itself be null; it should always point to an Optional instance.


    purpose of Optional is to tackle NullPointerException exception.

    Aside: Just to call it out clearly, that the choice would of course implicitly let you define if an NPE should be thrown at runtime or not. It's not determined at the compile time though.

    0 讨论(0)
  • 2021-02-07 01:43

    the purpose of Optional is to tackle NullPointerException exception

    Yes, it is, but at usage time not at creation.

    So when you receive an Optional from a method then you can avoid NPE by using Optional.ifPresent, Optional.orElse,Optional.orElseGet and Optional.orElseThrow methods.

    But this is not the case when you're creating an Optional. Since it's your own method you have to know whether the object is nullable or not.


    The main point of Optional is to provide a means for a function returning a value to indicate the absence of a return value. See this discussion. This allows the caller to continue a chain of fluent method calls.

    Stuart Marks

    Please read this post for more detailed explanation.

    0 讨论(0)
  • 2021-02-07 01:52

    I think the main purpose is that using Optional.of() can throw null-pointer which is exactly what we need in some cases.

    0 讨论(0)
  • 2021-02-07 01:54

    After reading some answers and comments I think this explanation is missing. Consider a method like

    public Optional<String> name(Customer c) {
        return c.isNew() ? Optional.ofNullable(getName(c)) : Optional.of(getName(c));
    }
    

    Here you want to throw a NullPointerException if the customer isn't new and is supposed to have a name; your code is inconsistent if that's ever null. Yet the name may not yet exist if the customer is new, hence ofNullable in that case and the method returns Optional.

    0 讨论(0)
  • 2021-02-07 01:54
    • Optional.ofNullable is a wrapper around existing APIs that can return null. You would call it as a consumer of an API.
    • Optional.of/Optional.empty is for new code written with Optional in mind. You would return an Optional created with of/empty as a provider of an API.
    0 讨论(0)
  • 2021-02-07 01:56

    I think it's quite simple and clear with Javadoc:

    Optional.of(T value) is used when you are sure that there is never a null value and incase null value occurs than program throws NullPointerException and consider as bug.

    Optional.ofNullable(T value) is used when you know that there can be a null value and in case of it your program should behave normally.


    Why would people opt for Optional instead of normal if-else method for null checking?

    0 讨论(0)
提交回复
热议问题