You may not have heard of it. 10 Features in Java 8

Source: Internet
Author: User

All that you've heard before 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 you should have from the start.

I'm going to introduce 10 essential features that I think are worth knowing. There will be at least one or two of them. You want to give it a 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 this interface.

This allows you to add new functionality to an existing library without breaking the code. This is obviously an upgrade. 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 expansion of the library. But on the bad side, we'll see later that the interface method is querying this and converting it to a concrete class. Shivers ....

2. Process termination

Starting an external process is half of what you do-when the process crashes, hangs or consumes 100% CPU when you want to go back and 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 having to wait for the process to complete. The second more powerful approach is destroyforcibly (), which allows you to force the killing of processes that have timed out or are no longer needed.

3. Stampedlocks

There's something exciting going on now. No one likes synchronizing code. It's the culprit (especially an extensible app) that lowers the throughput of your app, or even worse – causes the app to hang up. Still, at some point you have no choice.

There are many ways to restrict multithreaded synchronization access to critical resources. One of the most famous is the read-write lock (Readwritelock) and its associated implementations. It reduces resource contention by allowing multiple threads to access the same resource but blocking the operations resources. This sounds good in theory, but actually the lock is pretty slow, especially when there are a lot of write threads.

Note: There are two categories of resource visitors in read/write locks: Reader and writer, reader-only operation of resources, writer can manipulate resources.

Java 8 introduces a new read-write lock called Stampedlock.  This lock is not only faster, it's more optimistic the lock provides a powerful set of APIs through which you can get a reader lock at minimal cost, even if you don't want a write operation during the selection. 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 discard.

This lock is a powerful tool that needs a special article to describe. I'm so excited about this new gadget that I'm dizzy – well done!

Please see here for more details.

4. Concurrent adders

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

5. Optional Values

Oh, null pointers, nightmares for all Java developers. From the beginning (or at least at 1965) it may be the most popular anomaly.

For reference from scale and Hashell, Java 8 has a new template called optional, which encapsulates a possible null reference. It's definitely not a silver bullet that ends with NULL, it's more of a way-let the API designer indicate from the code level (not the document level) that a null value might pass in a method or be returned from a method, so that the caller is prepared for a null value. Therefore, this can only work under the new API, and assumes that the caller does not let the reference escape from this encapsulation, resulting in unsafe dereference.

I have to say that I am ambivalent about this trait. On the one hand null is a big problem, so I tend to do everything before null occurs. But on the other hand I doubt whether it will succeed. This is because the use of optional requires a constant effort by the company, but it has no direct value. Unless it is vigorous, it is likely to be abandoned.

For more information about optional please click here

6. Annotate any thing

Another small improvement in the Java language is that annotations can now be added to almost everything in your code. Previously, annotations could only be added to a class-like or method declaration. Annotations with Java 8 can be added to variables and parameter declarations, not only when passing a value to a specified type, even when allocating space to a new object. This is part of using static Analysis and wizard tools (such as FindBugs) to make the Java language more user-friendly and focused (along with improvements in Java documentation Tools and APIs). This is a great feature, but more of the invokedynamic introduced in Java 7, its real value depends on what the community is doing 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 (for example, "How did I get this weird value?").

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

8. Folder traversal

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

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

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 why the new Securerandom.getinstancestrong () method of the strongest random number generator can be picked up automatically in the JVM. It lowers

The chance of getting a generator failure, or by default getting a weak generator that causes the key or encrypted value to be easily cracked.

Date.toinstant ()

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

We all know what to do if we are faced with this problem.

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

If you think that the article has any features that are not mentioned, or do not agree with our analysis? Please comment to us-this is the purpose of the publication!

You may not have heard of it. 10 Features in Java 8

Related Article

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.