JavaRush /Java Blog /Random EN /10 Java 8 features you haven't heard of.

10 Java 8 features you haven't heard of.

Published in the Random EN group
Lambdas, lambdas, lambdas. That's all you hear about when people talk about Java 8. But that's just one part. Java 8 has a lot of new features - some powerful new classes and idioms, and others just things that should have been there from the beginning. I wanted to move on to ten new features that I see as little gems that are definitely worth knowing. There's bound to be at least one or two among them that you'd like to try, so let's get started! 1) Default methods. New in the Java language, you can now add method bodies to interfaces ( default methods ). These methods are unconditionally added to all classes that implement these interfaces. This gives you the opportunity to add new functionality to existing libraries without breaking the old code. That's definitely a plus. On the other hand, it seriously blurs the line between the interface, which obliges certain conditions to be followed as a convention, and the class that follows them, as its private implementation. In the (right) right hands, this can be an elegant way to make interfaces smarter, avoid repetition, and extend libraries. In bad hands; we'll soon see methods in interfaces that call this and cast it to a concrete type. Brrr.... 2) Stopping processes. Starting an additional process is one of the things you do half-knowing that you will come back (debug) to debug it when the process (cracks) (crashes) aborts, (hangs) hangs, or uses 100% of the CPU. The Process class has now been armed with two new methods to help you control unruly processes. The first, isAlive() , allows you to easily check if a process is running without having to wait for it. The second and more powerful (warning!) is destroyForcibly() , which allows you to (shamelessly) forcefully (kill) stop a process that has exceeded its time (waiting or otherwise) or is no longer needed. 3) StampedLock. Now for something really exciting. Nobody likes to sync code. This is a surefire way to slow down your application's performance (especially on a large scale) or worse, cause it to freeze. And yet, sometimes you just don't have a choice. There are many idioms for synchronizing multi-threaded access to a resource. One of the most preferred is ReadWriteLock and related implementations. This idiom reduces contention by allowing multiple threads to use a resource while blocking only the threads manipulating it. It sounds great, but in practice this lock is so slow , especially with a large number of writing threads. This turned out to be so bad that Java 8 introduces a new RWLock called StampedLock . This lock is not only faster, but also offers a powerful API for optimistic locking, where you can obtain a read lock at a low cost, hoping that no written operation occurs during critical sections of the code. At the end of the section, you ask the lock whether recording occurred during that time, in which case you can decide whether to try again, escalate the lock, or give up. This lock is a powerful tool and deserves a post all about itself. I'm giddily excited about this new toy. Great! Additionally you can read Here . 4) Competing recorders. This is another little gem for anyone running multi-threaded applications. A simple and efficient new API for reading and writing counters from multiple threads. Sometimes, it's even faster than using AtomicIntegers. Pretty awesome! 5) Optional (or optional) values. Oh, null pointers, a headache for all Java developers. Perhaps the most popular of all exceptions, it has been around since very early times. At least since 1965 . Borrowing from Scala and Haskell, Java 8 has a new pattern, called Optional, for wrapping references that can be null. This is by no means a silver bullet that will do away with null, but rather a means for API designers to indicate at the code level (instead of documentation) that a null value can be returned or passed to the method, and the caller must prepare for it. So this will only work for new APIs, provided callers don't allow the reference to escape from the wrapper, where it could be unsafely dereferenced. I have to say that I'm pretty ambivalent about this feature. On the one hand, nulls remain a huge problem, so I appreciate everything that has been done to solve it. On the other hand, I'm quite skeptical that this will succeed. This is because using Optional requires a sustained effort from the entire company and has little immediate impact. Without strong enforcement, chances are it will stay on the left side of the road. Read more about Optional . 6) Annotate everything. Another small advancement in the Java language: annotations can be added to almost everything in your code. Previously, annotations could only be added to things like classes or method declarations. Since Java 8, annotations can be added to variable and parameter declarations, when casting a value to a specific type, or even allocating a new object. This is part of a concentrated effort (along with improvements to JavaDoc tools and APIs) to make the language more friendly to static analysis and measurement tools (such as FindBugs). This is a nice feature, but just like invokeDynamic introduced in Java 7, its real value will depend on what society does with it. 7) Overflow operations. Now, here is a set of methods, which should have been part of the core library from day one. My favorite hobby is debugging numeric overflows when ints exceed 2^32, and proceeding to create the nastiest and most unexpected errors (i.e. "how on earth am I getting this weird value?"). Again, there are no silver bullets here, but there are plenty of functions for manipulating numbers that throw out when they overflow in a less forgiving way than your standard +/* operator, which unconditionally overflows. 8) Walk through the catalogue. Iterating through the contents of a directory tree has long been one of those things that people Google - search (in which case you'll probably be advised to use Apache.FileUtils). Java 8 gives the Files class a facelift with ten new methods. My favorite is walk() , which creates a lazy thread (important for large file systems) to iterate through the contents of a directory structure. 9) Strong random generation. There's no shortage of talk nowadays about the vulnerability of passwords and keys. Software security is a complex business and prone to error. That's why I like the new SecureRandom method. getinstanceStrong() , which automatically selects the strongest random generator available for the Java Virtual Machine. This reduces the chances that you will fail or have a weak generator, which will make the keys and encrypted values ​​more susceptible to hacking. 10. Date.toInstant(). Java 8 introduces a completely new API for dates and times . This is pretty understandable since the existing one is not very good. Joda has essentially been going towards a Java API for dates and times for several years. However, even with the new API, one big problem remains - there is a ton of code and libraries using the old API. And we all know that they are here to stay. So what will you do? To achieve this, Java 8 did something rather elegant by adding a new method to the Date class called toInstant() that converts it into a new API. This allows you to make a quick jump to the new API, even when working with code that uses the old date API (and will continue to do so for the foreseeable future).
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION