One good reason to use it is that it makes your zeros very meaningful. Instead of returning zero, which can mean many things (for example, an error, or a failure, or empty, etc.), you can put the "name" in your zero. Take a look at this example:
allows you to determine the base POJO:
class PersonDetails { String person; String comments; public PersonDetails(String person, String comments) { this.person = person; this.comments = comments; } public String getPerson() { return person; } public String getComments() { return comments; }
}
Now let's use this simple POJO:
public Optional<PersonDetails> getPersonDetailstWithOptional () { PersonDetails details = null; if (details == null) {
Now we avoid using null and perform our checks with the optional, therefore its significant
public void checkUsingOptional () { Optional<PersonDetails> details = getPersonDetailstWithOptional(); if (details.isPresent()) { PersonDetails details = details.get();
thus, after all, this is a way to make null values meaningful and less ambiguous.
j2emanue May 6 '15 at 12:52 a.m. 2015-05-06 00:52
source share