We know that Java 8 adds some very useful APIs, one of which is Optional. If it is not a bit of a false exploration, it is just an understatement to think that it can gracefully solve the problem of nullpointexception, so the code began to write
Optional<user> User = ...
if (User.ispresent ()) {
return User.getorders ();
} else {
return Collections.emptylist ();
}
> So we have to say that our thinking is still in place, just instinctively think that it is just a wrapper for the User instance, as we wrote before
user User = .....
if (user! = null) {
return user.getorders ();
} else {
return Collections.emptylis T ();
}
At the time of the Rio Olympics, the news repeatedly mentioned flag problems, but I can not see what the problem, and then the small stars worship the central posture is not right. So we must not take for granted what we are accustomed to, in the slightest, we will not feel anything wrong, in other words, when we switch to Java 8 Optional, can not inherit the past null when the kind of thinking, should grasp the new, correct use of Java 8 Opt Ional the correct posture. Read the full text >>
Correct posture using the Java8 Optional