Optional Class to Handle null in Java


Use Optional to represent nullable values with a more functional approach, reducing NullPointerException.

Source Code

Optional optional = Optional.ofNullable(null);
optional.ifPresent(System.out::println); // No output
System.out.println(optional.orElse("Default Value")); // Outputs: Default Value
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments