Java 8 Optional. Why of and ofNullable?

后端 未结 8 1521
旧时难觅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: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.

提交回复
热议问题