I was reading about Java 8 Optional and wanted to know the correct approach of using it. Traditional way,
User user = getUser();
if (user != null) {
//perform action
}
With Optional,
Optional<User> userOptional = getUser();
if (userOptional.isPresent()) {
User user = userOptional.get();
//perform action
}
With optional it requires more lines of code. Can you tell me where it is beneficial ?