Java SE 8 added a new class to handle nullability -
Optional - but when should it be used?
Optional in Java SE 8
Optional is not a new concept, but an old one which often results in polarized opinions.
It was added to Java 8
and is used in limited places at the moment.
The class itself is a simple box around an object, which has a special marker for empty instead of using
Optional<String> optional = Optional.of("Hello"); Optional<String> empty = Optional.empty();
The goal of the class is to reduce the need to use
Optional.empty() takes the place of
One way to think of it is as a collection that holds either zero or one objects.
When you want to get the data out of the optional, there is a nice simple method,
but this will throw an exception if the optional is empty.
In most cases, you will want to call one of the other methods, for example to return a default value:
Optional<Person> personResult = findPerson(name); Person person = personResult.orElse(Person.GUEST);
One approach to using
Optional is to use it everywhere in your entire codebase and eliminate
I don't subscribe to this approach, and nor does Brian Goetz.
Of course, people will do what they want. But we did have a clear intention when adding this feature, and it was not to be a general purpose Maybe or Some type, as much as many people would have liked us to do so. Our intention was to provide a limited mechanism for library method return types where there needed to be a clear way to represent "no result", and using null for such was overwhelmingly likely to cause errors.
The key here is the focus on use as a return type.
The class is definitively not intended for use as a property of a Java Bean.
Witness to this is that
Optional does not implement
Serializable, which is generally necessary for widespread use as a property of an object.
Where it is used as a return type,
Optional can work very well.
Consider the case where you are finding the first matching element in a stream.
In this case, what should the stream API do if the stream is empty?
Optional makes it clear that this case should be considered when using the API.
For me, the pleasant surprise about
Optional is that it has three friends -
that wrap an
I've made good use of these as return types where previously I would have used
Not only is the increase in meaning helpful, but the ability to call
orElse(0) to default
the missing value is a useful feature for API users.
My only fear is that
Optional will be overused.
Please focus on using it as a return type (from methods that perform some useful piece of functionality) Please don't use it as the field of a Java-Bean.
(As a side note, one reason not to use it in this way is that it creates an extra object for the garbage collector. If used as a method return type, the extra object is typically short-lived, which causes the gc little trouble. But when used in a Java-Bean, the object is likely to be long-lived, with a memory/gc overhead. I was bitten by something very similar to this about 13 years ago, and I can categorically say that for server systems running at scale, you don't want lots of little object boxes within your beans. Interestingly, if
Optional becomes a value type in a future Java version, then the memory/gc issue goes away.)
Optional in Java SE 8
Optional, and its three primitive friends, is very useful when used as a return type
from an operation where previously you might have returned
As written, the class is is not intended for use as a Java-bean property - please don't use it for that.