New features in Java 8
Java 8. |
The Java 8 release, Java contributed in support for functional programming, enhancement of JavaScript engine, outstanding APIs for date time manipulation, new streaming API, etc. There are dozens of notable and valuable features in Java 8, but the most significant features are discussed.
Out of all features, some are very talked-about amongst developers, while few are not talked about (quiet) features.
Java 8 Features
1. Lambda Expressions
As per Java developer's perspective, Lambda expression is one of the significant add-on features to the language. This extended feature brought Java App Development to the forefront of functional programming, just like other functional JVM-based languages - Scala and Clojure. A lambda expression is a function used to create delegates or expression tree types. Using lambda expressions, developers can code local functions which can be passed as arguments or returned as the value of function calls as lambdas are integrated into core language libraries.
Below example shows snippet of with and without lambda expressions. It mainly represents the difference in function calling ways –
//without lambda, Drawable implementation using an anonymous class
Drawable d=new Drawable(){
public void draw(){System.out.println("Drawing "+width);}
};
//with lambda
Drawable d2=()->{
System.out.println("Drawing "+width);
};
2. Concurrent Accumulators
In Java-based solutions, it is a very common scenario to use numeric counters which are accessed by multiple threads. In earlier Java versions it was difficult to modify the counter values. Java 8 resolved this issue with its concurrent accumulator classes, where the value can be increased/ decreased effectively in a thread-safe method. Some concurrent API enhancements are mentioned as follows:
ConcurrentHashMap – compute(), forEach(), forEachEntry(), forEachKey(), forEachValue(), merge(), reduce() and search() methods.
CompletableFuture – that may be explicitly completed (setting its value and status).
Executors newWorkStealingPool() – method to create a work-stealing thread pool using all available processors as its target parallelism level.
3. New Date/ Time API
Current native Java library API is complex to implement and execute, so Joda time is a boon for Java developers in such situation. But, with the coming of Java 8, the pain is cured as Java 8 came up with its own new Data/Time API under java. time package. New API is designed keeping simplicity in mind and it is easily readable to human and machine time formats. i.e Simplified and Specialized date/ time API for local and zonal time zones respectively.
4. Stream API
A stream, a new abstract layer, provides a set of elements of a particular type in a consecutive manner. A stream executes element on demand, it never stores the elements. Alike SQL statements, data can be processed in a declarative way by using stream also. It is similar to an iterator which allows a single run over a collection. It is the best feature for developers who work on Collections and Big Data. Running of streams can be either sequential (stream() method) or parallel (parallelstream() method). Parallel enhances the power of multiple cores.
5. Nashorn
Nashorn is a JavaScript engine which enables the developer to run the script on a JVM. Compare to Rhino, its performance is 2 to 10 times better as it uses to invoke the dynamic feature. It is also suitable for Node.js applications along with supporting actual Java libraries to be called by the JavaScript code executing on a server. JAVA 8 introduces a new command-line tool, jjs, to run JavaScript codes at a console.
6. Stamped Locks
Java 8 advances ReadWriteLock (implemented by ReentrantReadWriteLock) with boosting speed called StampedLock. It has an "optimistic" mode that arises a stamp that is returned by each locking operation to assist as a kind of admission ticket; each unlocks operation requires to pass its correlating stamp. The process results in faster execution. In a situation where readers are more than writers, use StampedLock to improvise the performance.
Comments
Post a Comment