5

I am new to Guava library.

I am trying to use Optional in my method arguments. One problem I found is that I cannot possibly pass null value into Optional.

I think the purpose of introducing Optional is to differentiate

  1. Something that does not have a value
  2. Something that has a null value

For example, Optional.absent() means the value does not exist. While null is a value that exist.

With this logic, I assume Optional must have some way to allow us to save a null value in it. However, I could not find a way to do this.

My method is defined as:

void myMethod(Optional<String> arguments) {
    ....
}

If I use

myMethod(Optional.of(null));

It will give me the runtime error says the value cannot be null.

How can I possibly pass null inside an Optional?

Kevin
  • 5,972
  • 17
  • 63
  • 87
  • 3
    You are completely missing `Optional`'s point, please read [UsingAndAvoidingNullExplained page from Guava Wiki](http://code.google.com/p/guava-libraries/wiki/UsingAndAvoidingNullExplained). Generally `Optional` is **not** meant to be used in method arguments but as a result. – Grzegorz Rożniecki Aug 31 '13 at 21:20
  • @Xaerxess where does it say that `Optional` is not meant to be used in method arguments? – darrengorman Aug 31 '13 at 21:29
  • @milkplusvellocet 1) While introducing `Optional` to Java 8 [there has been a big discussion about this](http://mail.openjdk.java.net/pipermail/lambda-dev/2013-June/010024.html): should`Optional` be treated as `OptionalResult`? 2) please read Oracle's [Brian Goetz summary on this topic](http://mail.openjdk.java.net/pipermail/lambda-dev/2012-October/006365.html). 3) [Guava devs also does not recommend using `Optional` in this way](http://stackoverflow.com/a/9561334/708434): _we recommend that you use Optional as a return type for your methods, but not necessarily in your method arguments._ – Grzegorz Rożniecki Aug 31 '13 at 21:35
  • @Xaerxess true that the Guava devs don't actively recommend it - perhaps OP has a good reason to use it in this way though. – darrengorman Aug 31 '13 at 22:07
  • The main reason that `Optional` doesn't make sense as a parameter is that there are better ways of making a parameter optional: primarily, creating an overload of the method that simply does not take the parameter. – ColinD Aug 31 '13 at 22:26
  • Is `Optional` in Java 8 or `Guava` specifically? –  Jul 19 '16 at 16:41

4 Answers4

1

See JavaDoc

An immutable object that may contain a non-null reference to another object. Each instance of this type either contains a non-null reference, or contains nothing (in which case we say that the reference is "absent"); it is never said to "contain null".

[...]

Community
  • 1
  • 1
jlordo
  • 37,490
  • 6
  • 58
  • 83
1

I think this is an intentional restriction.

Optional is an implementation of the Maybe monad. This is intended to replace nulls in a type-safe way, guaranteeing that if the option value is present, you won't get a NullPointerException when you try to use it. Allowing you to insert null would break this type-safety guarantee.

If you really need to distinguish two kinds of "no data" value, consider using Optional<Optional<String>> instead (wrapping your inner possibly-null data in an Option<String> by using Optional.fromNullable).

Mechanical snail
  • 29,755
  • 14
  • 88
  • 113
  • It's not fully parametric so I wouldn't say it forms a Monad. Not allowing the insertion of `null` actually breaks type-safety (exactly because we do a non-parametric `null` check). – Brian McKenna Aug 14 '15 at 05:08
1

Use Optional.fromNullable(T nullableReference)

darrengorman
  • 12,952
  • 2
  • 23
  • 24
  • 1
    This creates an absent Optional, not an Optional that contains a null value as asked in the question. – Dušan May 26 '17 at 12:46
  • @Dušan what would an Optional that contains a null value look like? – darrengorman May 26 '17 at 13:07
  • 1
    to put it simply `Optional.fromNullable(null)` is the same as `Optional.absent()` – Dušan Jun 01 '17 at 15:56
  • right, but you mentioned before that my answer results in an absent Optional, not an Optional that contains a null value. But the two are equivalent, so I'm not sure what your point was? – darrengorman Jun 02 '17 at 08:44
  • My point is that Optional can never contain a null value. Consider that `Optional.fromNullable(null).get()` will throw an exception, it will not return null. – Dušan Jun 12 '17 at 14:17
  • agreed. i never claimed it would return null. the original question asked "how can I pass nulls inside an Optional" which I answered. your original comment seemed to suggest I didn't answer the question... – darrengorman Jun 12 '17 at 14:42
-1

From this wiki from google/guava repo, you should use

Optional.absent()

@Dušan already wrote it in a comment to another answer, but I guess this way it will be easier to be found by other people

Daniel Reina
  • 5,764
  • 1
  • 37
  • 50