Everything you've heard about Java 8 is centered around lambda expressions. But it's just part of Java 8. Java 8 has many new features—some powerful new classes and syntax, and other things that should have from the beginning.
I'm going to introduce 10 essential features that I think are worth knowing. At least one or two of them will also be there for you to try, so let's get started!
1. Default method
A newly added feature of the Java language is that you can add method bodies (called default methods) to methods of interfaces. These methods will be implicitly added to the class that implements this interface.
This allows you to add new features to existing libraries without breaking your code. This is obviously an improvement. But on the other hand, this obfuscates the boundaries between interfaces and classes, because interfaces are used to define contracts, while classes are used to define concrete implementations. On the plus side, it makes the interface smarter in an elegant way and avoids duplication and extension of libraries. But on the bad side, we will see later that the interface method queries this and converts it into concrete classes. Shivers….
2. Process termination
Starting an external process is half of what you do - when the process crashes, hangs, or consumes 100% CPU, you need to go back and debug it. The Process class is now equipped with two new methods to help you control unruly processes.
The first is isAlive(), which allows you to check whether the process is still running without waiting for the process to complete. The second more powerful method is destroyForcibly(), which allows you to force kill processes that are timed out or no longer needed.
3. StampedLocks
There is something exciting now. No one likes to sync code. It is the culprit for reducing the throughput of your application (especially scalable applications), or more severely causing the application to hang up. Still, at some point you have no choice.
There are many ways to limit access to critical resources in a multithreaded synchronous manner. The most famous of these are read-write locks (ReadWriteLock) and implementations related to it. It reduces resource competition by allowing multiple threads to access the same resource but blocking threads operating the resource. This sounds good in theory, but in fact this lock is quite slow, especially when there are a large number of write threads.
Note: The read-write lock divides resource visitors into two categories: reader and writer. The reader performs read-only operations on the resource, and the writer can operate the resource.
Java 8 introduces a brand new read-write lock called StampedLock. This lock is not only faster, it provides a powerful set of optimism locks, through which you can obtain a reader lock at the minimum cost, and even do not want write operations during the selection. After the selection is over, you can query the lock to see if there is a write operation during the selection, in which case you can choose whether to try again, update the lock or give up.
This lock is a very powerful tool and needs a special article to describe it. I was so excited about this new thing that I was dizzy and well done!
For more details, please refer to here
4. Concurrent Adders
This is another little baby for those working in multithreaded applications. This is a simple and efficient new API for multithreaded counters, which is faster than using AtomicInteger. So fucking cool!
5. Optional Values
Oh, null pointer, a nightmare for all Java developers. From the beginning (or at least in 1965) it was probably the most popular exception.
Drawing from Scale and Hashell, Java 8 has a new template called Optional for encapsulating references that may be null. It is by no means a silver bullet to terminate null, it is a way to let the API designer indicate from the code level (rather than the document level) that a null value may be passed into a method or returned from a method to prepare the caller for the null value. Therefore, this can only work under the new API, and assuming the caller does not let the reference escape the encapsulation resulting in unsafe dereferences.
I have to say I'm very conflicted with this feature. On the one hand, there is a big problem with null, so I tend to do everything well before null happens. But on the other hand, I doubt it will succeed. This is because using Optional requires constant efforts from the whole company, but it has no direct value. Unless it is decisive, it is likely to be abandoned.
For more information about Optional, please click here
6. Annotate anything
Another small Java language improvement is that annotations can now be added to almost everything in your code. In the past, annotations could only be added to the class or method declaration. Annotations using Java 8 can be added to variable and parameter declarations, not only when passing a value to a specified type, but even when allocating space to a new object. This is part of making the Java language more friendly and focused (along with Java Documentation Tools and API improvements) through static analysis and wizard tools such as FindBugs. This is a great feature, but more of a thing like invokeDynamic introduced in Java 7, and its real value is what the community does with it.
7. Overflow operation
Now here is a series of methods that should be included in the core library from the very beginning. One of my favorite habits is to debug overflows when int exceeds 2^32 and then continue to create this infamous bug at random (such as "How did I get this weird value?").
There is no silver bullet this time, but there are a series of functions that operate numbers that throw exceptions when overflow occurs in a way that is more tolerant than the standard +/* operator that implicitly causes overflow.. If it were me, I would set it as the default mode of the JVM, using explicit functions that allow arithmetic overflow (rather than the +* operator).
8. Folder traversal
The content of iterating directory trees has appeared very early on Google searches (in this case you might use Apache.FileUtils). Java 8
Added 10 new methods for Files. My favorite is walk() , which creates a lazy stream (important for large file systems) to iterate
Contents of the directory structure.
9. Strong random number generation
Today's talk about the vulnerability of passwords and keys is a must. Program security is a tough problem and it's easy to make mistakes. This is also what I like
The reason for the new SecureRandom.getinstanceStrong() method that can automatically select the strongest random number generator in JVM. It reduces
The chances of a generator failure or a weak generator are obtained by default, resulting in a chance that the key or encryption value is easily cracked.
10. Date.toInstant()
Java 8 introduces a brand new date time API. Because the existing API is not good, this is quite understandable. Now Joda has focused and penetrated the Java 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 that use the old API.
We all know that facing this problem, so what should we do?
Because Java 8 has done quite elegantly, a new method toInstant() was added to the Date class to convert it into a new API. Even using the old Date API (same for the foreseeable future), it will allow you to quickly upgrade to the new API.
What do you think the article has other characteristics that should not be mentioned, or do you disagree with our analysis? Please comment to us - this is the purpose of posting!
The above is a compilation of the information on the new features of Java 8. We will continue to add relevant information in the future. Thank you for your support for this website!