I understand that Optionals ifPresent() call is meant to replace a null check. Pulling code examples from the Oracle documentation, it appears to be very useful in simple situations. For example:
Optional<Soundcard> soundcard = ...;
soundcard.ifPresent(System.out::println);
I just want to understand why this is considered better than a null check. Readability? Performance? It seems to me that this would cause a hit to project performance, as a new object must be introduced in order to hold the object we ultimately wish to obtain? In full, why is this soundcard.ifPresent()
considered better than this if(soundcard != null)
.