Five development tips for Java 8

Source: Internet
Author: User

Reprint: http://geek.csdn.net/news/detail/94219

Before Java 9 was released, let's share some Java 8 development tips that are translated from the Java 8 Top tips of JetBrains Senior development director Trisha Geed.

"The following is the translation"

After years of development using Java 8, combined with personal use of IntelliJ idea, I summarized the following several JAVA8 techniques for your reference.

Optional

Optional is a highly underrated trait that, if applied properly, can actually help reduce the number of nullpointerexceptions (invalid pointers) errors. Here are two key points to use for optional:

    • Optional should be used only for return types instead of parameters or fields

If you try to do this, IntelliJ idea will give you the following tips:

    • Avoid using the called Get ()

Optional's strength is to express the null value and let you deal with it. Therefore, it is important to check the values before proceeding to the next step. Fortunately IntelliJ idea will also make a hint:

Instead, you can try using OrElse or Orelseget.

Use of lambda expressions

Lambda is another highlight of Java 8 and the following are tips for using lambda:

    • As simple as possible

Simplification is the key to the correct use of lambda expressions. You can simplify them to a single line, or you can refactor more complex expressions in a method.

    • Clearly understood

Because lambda expressions lack type information, the type information is sometimes put into parameters:

This is too lengthy, how to do? I prefer to have a meaningful name for the parameter. For example:

Design of lambda expressions

While we are less likely to create a method with lambda as a parameter, how do we deal with it if there is such a requirement?

    • IntelliJ idea helps to implement function-type parameters

    • Add @functionalinterface to your functional interface

If you need to customize the functional interface, then IntelliJ idea will be a good watchdog:
Overlay hints:

Duplicate declaration:

defined for a class other than an interface:

Streams (Stream)

The Stream API is a new introduction to Java8, where is the use of it?

    • Orderly arrangement

    • Using method references

    • Use the streams API whenever possible for collection recursion

Please compare the following two sections of code:

Using a For loop in an array loop

Using a For loop in an array of metadata types will give you the best performance:

Summary

I'm looking for new ways to try new ideas every day, hoping to keep improving the best practices for personal programming.

Five development tips for Java 8

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.