Languages
[Edit]
EN

Java - why java.util.Optional is not serializable?

5 points
Created by:
isherwood
599

1. Solution for common problems with Optional serializable exception

JDK Optional doesn't implements Serializable interface. So how can we work with this Optional when dealing with serialization?

Let's take a look at this example:

private final Optional<Integer> age;

public Optional<Integer> getAge() {
	return age;
}

Instead of using Optional as field like we see above, we can use Optional as return type from getter and create new instance on each getter invocation.

private final Integer age; // diff1: without Optional

public Optional<Integer> getAge() {
    // diff2: each method invocation return new Optional
    return Optional.ofNullable(age);
}

Check this link to see how to solve common java serialization problem with full code example that I described above:

this link

2. Guava serializable Optional

If we want to use serializable Optional, we can use Guava's Optional.

package com.google.common.base;

public abstract class Optional<T> implements Serializable {
    // ...
}

For contrast we can compare java.util optional internals:

package java.util;

public final class Optional<T> {
    // ...
}

3. Shouldn't Optional be Serializable?

Answer from Brian Goetz (JDK architect from Oracle)

The best parts from the emails:

Part 1

Someone suggested maybe even renaming it to OptionalReturn 
to beat users over the head with this design orientation

Part 2

>> Presumably because you may want to have class fields that express
>> nullability via Optional rather than null.
> 
> Using Optional as a field type doesn't seem to offer much.
> 
> Would the Optional field be wrapped in accessors? 

Part 3

>>> wrong question.
>>> the right one is why do you want Optional to be Serializable.

Full email list:

Brian Goetz brian.goetz at oracle.com
Fri Sep 27 17:46:18 PDT 2013
Previous message: Shouldn't Optional be Serializable?
Next message: Shouldn't Optional be Serializable?
Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Amplifying Mike's observations: this exploration is already covered ground.  
The JSR-335 EG felt fairly strongly that Optional should not be on any more 
than needed to support the optional-return idiom only.  (Someone suggested 
maybe even renaming it to OptionalReturn to beat users over the head with 
this design orientation; perhaps we should have taken that suggestion.)  I 
get that lots of people want Optional to be something else.  But, its not 
simply the case that the EG "forgot" to make it serializable; they explicitly 
chose not to.  And there's certainly been no new data that has come to light 
that would motivate us to set that choice aside at this point.  



On Sep 28, 2013, at 2:15 AM, Mike Duigou wrote:

> 
> On Sep 17 2013, at 16:32 , Vitaly Davidovich wrote:
> 
>> Presumably because you may want to have class fields that express
>> nullability via Optional rather than null.
> 
> Using Optional as a field type doesn't seem to offer much.
> 
> Would the Optional field be wrapped in accessors? 
> 
> If it's not wrapped, the result is that orElse(default) gets spread across 
the usage of the field which is a poor way to apply a default.
> 
> If if it is wrapped, why not apply the default in the accessor or in the 
setter and not bother with Optional?
> 
>> Whether that's a good design or
>> not is a separate question;
> 
> That's not generally how JDK expert groups think. Adding any feature to 
intentionally support bad design wouldn't make much sense.  Understanding the 
dimensions of how a feature might be used is certainly something that the EG 
spends a lot of time on. The choice of semantics, methods offered and 
interfaces implemented isn't ever separated from the expected use cases. 
Optional is a new class/feature to the JDK and it seemed appropriate to start 
with a minimal implementation.  Concern that Optional would be misused in 
other use cases threatened to derail it's inclusion in Java entirely!  
Optional is being added for the value it offers in "fluent" sequences of 
statements.  In this context use of Optional as a visible type or for 
serialization isn't relevant.
> 
>> conceptually, I don't see a reason why Optional
>> cannot support that.  For "reference", Google Guava's version is
>> serializable.
> 
> Guava's Optional certainly proves that it is technically possible (which 
was probably already known anyway) but that doesn't address whether doing so 
with the Java Optional would be a good idea or not.
> 
>> If someone were to replace their use with jdk's Optional
>> then they will hit exceptions if the owner class is serialized.
> 
> While the incompatibility is certainly unfortunate this is probably not a 
sufficient reason by itself to make Java's Optional serializable.
> 
> Mike
> 
>> Sent from my phone
>> On Sep 17, 2013 6:06 PM, "Remi Forax" <forax at univ-mlv.fr> wrote:
>> 
>>> On 09/17/2013 11:44 PM, Pete Poulos wrote:
>>> 
>>>> Shouldn't java.util.Optional be Serializable?  Is there a good reason for
>>>> it not be?
>>>> 
>>> 
>>> wrong question.
>>> the right one is why do you want Optional to be Serializable.
>>> 
>>> Thanks,
>>>> Pete
>>>> 
>>> 
>>> cheers,
>>> Rémi
>>> 
>>> 
> 

References

  1. Shouldn't Optional be Serializable? - Brian Goetz
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join