Correct posture using the Java8 Optional

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.