Introduction
Java 8 is the latest version of Java. It may be the most awaited version in Java history. Java 8 was set to be released in September 2013, but due to some technical changes it will probably be released in March 2014. It will include Lambda, functional interfaces, streams and much more. Let's talk about the new things in Java 8.
Lambda
Java 8 includes lambda expressions. Lambda expressions can instantiate functional interfaces in Java 8. Lambda expressions are able to pass code to methods rather than objects. They will replace the use of callbacks in Java. Callbacks have a disadvantage, in other words we have to write code of five line but our work is only in one line out of the five lines.
Examples of Lambda expressions:
Input is on the left and the code is on right.
( int x, int y ) -> { return x+y; }
x -> x * x
( x, y ) -> x+y
( ) -> x
x -> { System.out.println(x); }
Examples of method references with Lambda expressions as the substitute:
String: :value of x -> String.value of(x)
Object: :to string x -> x.toString( )
ArrayList: :new ( ) -> new ArrayList<>( )
Previously used code:
btn.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
System.out.println("yahoo");
}
}
Using Lambda expressions:
btn.setOnAction(event -> System.out.println("yahoo"));
Generic type inference improvements
The ability of the Java compiler to reduce explicit type arguments and to infer generic types has been improved.
Code in Java 7:
doo(Utility.<Type>bar());
Utility.<Type>doo().bar();
Code in Java 8:
doo(Utility.bar());
Utility.doo().bar();
IO/NIO additions
These additions gives us the path to java.util.stream. These streams are different from the regular streams. There is a IOException that extends RuntimeException and also a CloseableStream that should be closed.
List of additions:
- Files.list( Path )
- Files.walk( Path, int, FileVisitOption )
- BufferedReader.lines( )
- Files.lines( Path, Charset )
- DirectoryStream.stream( )
Collections API additions
Java 8n adds a large number of methods to the collections API just because of the ability of the interfaces to have default methods. All the interfaces were provided default implementations and the concrete classes were added more efficient implementations.
List of some new methods:
- Collection.stream( )
- Iterable.forEach
- Map.forEach( )
- Map.remove( Object, Object )
- Map.replaceAll( )
- List.replaceAll( )
- List.sort( )
- Collection.spliterator( )
Annotation and reflection changes
Annotations are useful for static analysis tools such as Sonar and it also enhances error detection.
List of annotations:
- AnnotatedType
- AnnotatedArrayType
- AnnotatedTypeVariable
- Class.getTypeName( )
- Executable
- Native
- Class.toGenericString( )
- Repeatable
Java Time
Initially we have the java.util.Date class. Then we had java.sql.Date that helped us for time and place using fully qualified names in code. Now in Java 8 we have the java.time package. It is very well designed and easy to learn. All the classes in this package are thread safe. It includes local date, local timeZoned date time, year, month, day, offsettime and offsetdatetime. It is almost completely supported by JDBC.
Web View Enhancements
The Nashorn JavaScript engine is added to the web view. It implements a high performance and lightweight JavaScript that has a native JVM. It is integrated into the JDK. It is the successor to Rhino. It is improved in performance and memory usage than Rhino. It will not support DOM/CSS but will support the javax.script API.
Concurrency API additions
Some additions have also been provided for the concurrency API. All parallel stream operations are handled by ForkJoinPool.commonPool( ). Any ForkJoinTask can use the common pool. ReentrantReadWriteLock is replaced by Stamped Lock. ConcurrentHashMap is completely rewritten. CompletableFututre is the replacement of Futures, ListenableFuture, SettableFuture.
Rich Text Support
It is useful in applying transforms and embedding nodes inside the text. It can create text that can be styled with the help of cascading style sheets. It is also very useful in applying effects and style to individual words.
Printing
Printing will be supported by JavaFX.
Audio and Video recording
Audio and Video recording will be supported.
New Looks
Looks are improved and a new theme is introduced known as Modena.
3D support
3D support will be improved and accurate. But 3D features will not be available when JavaFX is running with software rendering.
Summary
This article introduces you to Java 8 and shows you some new features added to this newer version of Java.