1

I know we have Optional.ofNullable() if the value can also be null. My question is, why isn't that the default behaviour for Optional.of() as well? If a value can not be null, why put in in the Optional in the first place? If it turns out to be null anyway, you will get an NPE all the same anyway. If you construct an Optional with the of() method, you will still have all the other methods of the Optional wrapper which only really have a purpose if the value can be null.

Edit: I have read Why use Optional.of over Optional.ofNullable? before posting this, but the answer there was "because you catch the error sooner, and it's easier to trace it that way". But I kinda disagree with that, maybe you will have a few extra lines in your stacktrace, but it's still easy to pinpoint the source of the NPE.

logi0517
  • 813
  • 1
  • 13
  • 32
  • FWIW this should not have been closed. Your question is totally different than the ones this was linked to. The answer is: the Java committee messed up. The Optional class cannot be used in the one way it is most needed: for composable maybe-absent values. You can't have Optional<@Nullable String>. You can compose optionals, but Optional is actually Optional which is less general than Optional. It is a design error in Java. – Kenn Knowles May 01 '23 at 18:34

2 Answers2

5

That method is meant to be used for non null parameters! The intent of that method is to guarantee that it will fail when called with null!

It is about the programmer communicating that intent to the reader!

When you don't know what is coming in, and that input might be null, then you tell that to the reader by using that other method. But when you know "an incoming null should never occur" then it might be fair to throw an exception when that very strict contract is violated.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Okay I think that's an okay reason, after all without using null, if the programmer calls a method on the null object, he/she might have just forgot the null check. That cant be said, when using Optional.of() – logi0517 Apr 22 '19 at 19:07
  • 1
    Excellent point! I saw an NPE getting thrown in my log so I changed the code from .of() to .ofNullable() then read your answer and realized OH NO it might have been on purpose! But I looked at what is calling the method which returns this Optional and realized since we were doing isPresentOrElse this means we meant to use .ofNullable there. – LConrad May 21 '20 at 18:57
  • I get it... but it would have been better for the default behavior for 'Optional.of' to be the current behavior of 'Optional.ofNullable' and instead add the method 'Optional.ofNonNull(foo)' -- this would be less confusing to people. – davesbrain Oct 19 '22 at 19:54
  • The whole point of the overall exercise is to avoid Nulls where possible. I think it is somehow reasonable to also express that within this api. In the end, this is a style choice. Some people like it this way, others might prefer your idea. – GhostCat Oct 20 '22 at 08:53
  • This is not the whole answer at all. You cannot create an `Optional<@Nullable String>` for example. That is a very obvious and normal use of a well-designed `Optional` class. The one that ships with Java is just not well-designed. – Kenn Knowles May 01 '23 at 18:36
  • What is the "whole" answer then? – GhostCat May 03 '23 at 04:25
-1

It can't be null. Optional.ofNullable(null) returns an Optional where isPresent is false, not one where it's true but with a null in it.