I have question with Java 8 Optionals.
The below code gives compilation error:
Integer number = Optional.ofNullable(new Integer(10));
But when we do the following it does not, I don't understand how this is working:
Integer number = Optional.ofNullable(new Integer(10)).orElse(10);
If we look at the documentation of Optional.ofNullable(), it returns an object of the type static <T> Optional<T> ofNullable(T value) which signifies that it is internally type casting, but the documentation says it return type is Optional.
I get that the .orElse method return type is T which would work fine if it goes to orElse condition, i.e in case the passed in object is null, but if it is not null how does it typecast it?