In Java 8, they have improved the optimisation of object creation, especially for short lived objects such as Optional. What the JIT can do is use Escape Analysis to eliminate short lived objects by placing their fields on the stack. In the case of Optional<Boolean>
this can most likely be turned in no more than boolean
See the following article on object elimination, how to detect it is not working and what you can do about it. Java Lambdas and Low Latency
The converse question, is why have OptionalInt, OptionalLong and OptionalDouble? These are likely to be useful, just not as useful as you might think. Unlike Boolean
not all Integer
, Long
and Double
values are cached, and while Escape Analisys can eliminate objects, it is expensive and can take a while to kick in, possibly never for code not run long enough.