Summary and detailed explanation of 10 features in Java 8 _java

Source: Internet
Author: User
Tags generator

All that you've heard about Java8 is around lambda expressions. But it is only part of the JAVA8. Java 8 has many new features-some powerful new classes and grammars, and other things that should have been there from the start.

I'm going to introduce 10 essential features I think are worth knowing. There will be at least one or two of them you want to try, so let's get started!

1. Default method

A newly added feature of the Java language is that you can add a method body (called the default method) for an interface (interface) method. These methods are implicitly added to the class that implements the interface.

This allows you to add new functionality to existing libraries without breaking the code. This is obviously a promotion. On the other hand, this confuses the boundary between the interface and the class, because the interface is used to define the contract, and the class is used to define the concrete implementation. On the good side, it makes the interface smarter in an elegant way and avoids duplication and extension libraries. But the bad side, as we'll see later, is that the interface method queries this and converts it to a specific class. Shivers .....

2. Process termination

Starting an external process is half of what you do-when this process crashes, hangs or consumes 100% CPU you want to return it to debug it. Now the process class is equipped with two new ways to help you control unruly processes.

The first is isAlive (), which allows you to check that the process is still running without waiting for the process to complete. A second, more powerful approach is destroyforcibly (), which allows you to force the killing of a process that is timed out or no longer needed.

3. Stampedlocks

Now there's something exciting. No one likes to sync code. It is the culprit (especially the scalable application) that lowers the throughput of your application, or more seriously – causing applications to hang. Still, at some point you have no choice.

There are many ways to restrict multithreaded synchronization access to critical resources. One of the most notable is the read-write lock (Readwritelock) and the implementation associated with it. It reduces contention for resources by allowing multithreading to access the same resource but blocking the operation resource's threads. This sounds pretty good in theory, but actually the lock is pretty slow, especially if there's a lot of writing threads.

Note: The reading and writing lock will be divided into two categories of resource visitors: readers and writer, readers to the resources read-only operation, the writer can manipulate resources.

Java 8 introduces a new read-write lock called Stampedlock.  This lock is not only faster, it is more optimistic the lock provides a powerful set of APIs through which you can obtain a reader lock at a minimal cost, even when you choose not to have write operations. At the end of the selection you can check the lock to see if there is a write operation during the selection, in which case you can choose whether to retry, update the lock or give up.

The lock is a powerful tool that requires a dedicated article to describe it. I'm so excited about this new gadget--well done!

Please see here for more details

4. Concurrent adders

This is another little baby for people who are engaged in multi-threaded applications. This is a simple and efficient new API for multi-threaded counters, which is faster than using Atomicinteger. It's so damn cool!

5. Optional Values

Oh, null pointer, nightmare for all Java developers. From the start (or at least 1965) it may be the most popular anomaly.

Drawing on from scale and Hashell, Java 8 has a new template called optional that encapsulates potentially null references. It's not a silver bullet that ends in null, it's more of a way-letting API designers indicate from a code level (not a document level) that a null value might pass in a method or return from a method to make the caller ready for a null value. Therefore, this can only work under the new API, and assumes that the caller does not allow the reference to escape the encapsulation resulting in unsafe dereference.

I have to say I'm conflicted about this trait. On the one hand null is a big problem, so I tend to do everything before null happens. On the other hand, I doubt if it will succeed. This is because the use of optional requires the continuous efforts of the whole company, but it does not have direct value. Unless it is vigorous, it is likely to be abandoned.

More about optional please click here

6. Note anything

Another small improvement in the Java language is that annotations is now able to add to almost everything in your code. Previously, annotations could only be added to a class or method declaration. Annotations with Java 8 can be added to variable and parameter declarations, not only when passing a value to a specified type, but even allocating space to a new object. This is part of a Static Analysis and wizard tool (such as FindBugs) that makes the Java language friendlier and focused (along with Java document tools and API improvements). This is a great feature, but more like the invokedynamic introduced in Java 7, the real value decision lies in what the community does with it.

7. Overflow operation

Now there is a series of methods that should be included in the core library from the outset. One of my favorite habits is to debug an overflow when int exceeds 2^32, and then continue to randomly create this infamous bug (such as "How did I get this weird value?").

This time there is no silver bullet, but there is a series of functions that manipulate numbers that throw an exception when they overflow in a way that is more intolerable than the standard +/* operator that implicitly causes overflow. If it were me, I would set it as the default mode for the JVM, using explicit functions that allow arithmetic overflows (instead of the + * operator).

8. Folder traversal

The content of the iteration directory tree appears very early on Google search (in which case you might use Apache.fileutils). Java 8

10 new methods have been added for files. My favorite is walk (), which creates a lazy stream (important for large file systems) to iterate over

The contents of the directory structure.

9. Strong random Number generation

Today's conversation about the fragility of passwords and keys is short. Program security is a tricky problem and it's easy to make mistakes. That's what I like.

The reason for the new Securerandom.getinstancestrong () method that can automatically pick the strongest random number generator in the JVM. It lowers

The chance of getting the generator to fail, or to get a weak generator by default so that the key or encrypted value is easily cracked.

Date.toinstant ()

Java 8 introduces a new date time API. Because the existing API is not good, this is quite understandable. Now Joda has been focusing and delving into the Java date time API for years. However, even with the new API there is still a big problem-there are tons of code and libraries using the old APIs.

We all know what to do with this problem.

Because Java 8 has been quite elegant, a new method Toinstant () has been added to the date class to transform it into a new API. Even with the old date API (which is the same in the foreseeable future), you can quickly upgrade to a new API.

If you think there's something else in the article that doesn't mention it, or does it disagree with our analysis? Please comment to us--this is the purpose of the dispatch!

The above is the Java 8 new characteristics of the data collation, follow-up continue to supplement the relevant information thank you for your support of this site!

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.