I tried this line while experimenting:
String test = Optional.empty().orElse("test");
--and to my surprise it gave a type error: incompatible types: Object cannot be converted to String.
Why is that? Looking at the source, orElse
should just return what's being passed in if the optional is empty, which it is here. So what am I missing? This works fine, on the other hand:
String test = Optional.of("me").orElse("test");