Java Evolution-JAVA8 Real-Combat notes

Source: Internet
Author: User

A language to always have vitality, it also needs to follow the changes of the Times to progress, Java as an ancient language, it actually has too many historical baggage, in the process of change need to consider a lot, but it is also slowly evolving to consolidate their walls, Don't let yourself be forgotten in history (a lot of programming languages have disappeared in people's sights over time). Of course, as a programmer who takes the Java language as the Master language, its progress is actually extending our career.

Java8 brings a lot of new features, although Java8 has been released for a long time, but there has been no systematic study, so this part has not been summarized. The former owners considered compatibility with previous systems, and the technical standards would be JDK7 as a development tool, which would not take advantage of the new features that JAVA8 brings. In fact, Java as a widely used language, its own release cycle has been considered forward compatibility, so even if upgrading the JDK version, in fact, will not bring too much trouble, follow the latest technology, so that employees can learn the new technical standards, in fact, let employees more stable strategy, At the same time, while solving the problem, both get the exercise, but also get a sense of accomplishment. This is also important.

Function
    1. The introduction of JAVA8 Lambda can be said to be a very important feature, it can be said that Java has a new value of the property, the type of the value is a function (this feature in other languages is very early support, Java is actually a bit slow motion).

So when you want to

    • Sort a Collection
    • Declare a thread
    • ...

There is no need to implement an anonymous class, anonymous class too much useless code, the direct transfer of the lambda function is more concise, easy to read.
Lambda declaration method (arg1, arg2,...) -> {statement} , where a sentence return expression; can be abbreviated to (arg1, arg2,...) -> expression .

    1. Java also introduces a syntax sugar that references an existing method, which is called a method reference, and can further simplify the code.
    • static method Reference
    • Point to any type instance method reference
    • Method reference pointing to an existing object instance
    1. Now there are parameters passed in, but if declaring a function's arguments is how the lambda type is to be declared, then a function interface is required, the type of lambda is implemented in Java as a function interface, and the function interface is an interface with only one abstract method. Java has already provided some function interfaces that can be used directly, and when we do not meet the requirements we declare our own function interfaces.
Stream

The power of stream is that it allows you to work with datasets in a declarative way, and if the data processing process, the processor, and the collector (which are described later) have no side effects (a concept in functional programming ideas), stream can be a simple, easy-to-use multi-core architecture Advantage , automatic parallel execution. So it's two powerful advantages: declarative data processing, automatic parallel execution. Let's take a look at the advantages of these two streams, and when you're familiar with them, you'll be tempted to put them down.

If you understand the idea behind Rxjava, in fact, this and Rxjava use very similar, of course, Rxjava function than stream to be more powerful, but also use this figure to think about data processing, Rxjava is a model to simplify asynchronous programming, If you do not know can go to study, very convenient one tool. There is a lot of talk here, now in the high-concurrency environment, the demand for asynchronous more and more, so in the IO-intensive scenarios to save resources, can support high concurrency, but asynchronous programming a disadvantage is not consistent with people's thinking habits, so it is very complicated to write. The thought of Rxjava must have reduced this inconvenience.
But the new language go has solved this problem from another angle, it supports the concept of the process in runtime, you can understand that the process is to share a thread of several blocks, the programmer to manage the transition between the process. The advantage is that the thread is fully utilized and the user is allowed to write code in a synchronized model, which solves the habit of not conforming to people's thinking in asynchronous programming. Of course this is just my vulgar understanding, go language is worth learning language, can be taken as an alternative language learning.

The operation on Steam can be divided into two major types of operations

    • Intermediate operation

    • Terminal operations

There is a very handy terminal operation on the stream, which is the collector. Some of the predefined collectors are available in Java, which are Collector created by the factory method provided by the class and are categorized by functionality as follows.

    • Sum the flow elements to a value
    • Element grouping
    • Element partitioning

The parallel data processing of stream is not mentioned in this part, which involves the implementation of the lower level of parallel stream, but also how to customize the split stream, need to understand its principle to use correctly. For the time being, remember that stream can transparently parallel data processing to improve program efficiency.

Default method

Java history has been a long time, but in order to let the collection support stream interface, we need to provide a stream method for the collection, but there are many third-party libraries implemented the collection interface, if you rashly add methods in the interface, it will break the forward-compatible properties. So JAVA8 provides the default method to keep forward compatible.

    • Java8 allows static methods to be declared within an interface
    • Java8 introduced by default method, you can specify the default implementation of the interface method (return type before using the default adornment)

However, the introduction of the default method introduces some conflicts in some cases, similar to the diamond inheritance problem in C + + multiple inheritance, here Java8 has a conflict resolution rule,

    1. The methods in the class have the highest precedence. A method declared in a class or parent class takes precedence over any precedence declared as the default method
    2. If the first rule cannot be judged, then the sub-interface has higher precedence: The function signature is the same, the interface that has the most specific implementation of the default method is preferred, that is, if B inherits A, B is more specific than a.
    3. Finally, if it is still impossible to judge, a class that inherits multiple interfaces must explicitly choose which default method to use by explicitly overwriting and invoking the desired method
Optional

Optional is a feature that prevents NullPointerException from appearing, in fact it gives us more code readability in terms of semantics, assuming that when a class's properties are packaged using optional, The person using this property can learn that this property might be null and use it with caution, but when a property is not wrapped in optional, that means I can use the reference with confidence without worrying about nullpointerexception. But this requires a team engagement.

Optional can also remove if else judgments in some places where it is necessary to determine if the reference is null to make the code more concise.

Completablefuture

This interface allows the flexible completion of the modular asynchronous programming, is a further enhancement of the future interface, but relative to the Rxjava asynchronous programming library, its semantics is still poor, but can be understood.

New Date and Time API

Another important issue addressed by JAVA8 is the resolution of previous versions of Java java.util.Date classes and other defects (variability, bad offset values, default values, and commands) for modeling date-time class designs
For a date-time operation in Java, try to use a java.time few new classes under the package, including LocalDate ,, LocalTime Instant , Duration and Period .

Java不断的革新自己使得自己适应现在的编程场景,作为一名技术人员当然也需要不断的更新自己的知识,保证自己也不断的跟随着浪潮前进。

Java Evolution-JAVA8 Real-Combat notes

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.