Why are Java Optionals immutable?

I would like to understand why the Java 8 Optionals were designed as immutable. Is it just for thread safety?

+8
java optional
source share
2 answers

Options are designed to transfer things. If you give something to someone, and then change its content, they will be very surprised, not in a good way.

+11
source share

Optional is considered an object of value and should be unchanged in order to reduce their ability to make you pull your hair out, tracking the error at 3 a.m. You can read a little more on the topic here:

http://martinfowler.com/bliki/ValueObject.html

http://c2.com/cgi/wiki?ValueObjectsShouldBeImmutable

+6
source share

All Articles