A New Version of Java - Java 8

In this article you will learn about java 8 and the features adding to java 8.

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 on September 2013, but due to some technical changes, it will probably be released on 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 the passcode 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:
  1. String: :value of        x  ->  String.value of(x)  
  2. Object: :to string      x  ->  x.toString( )  
  3. ArrayList: :new         ( )  ->  new ArrayList<>( )  
  4. Previously used code:  
  5. btn.setOnAction(new EventHandler<ActionEvent>()  
  6. {  
  7.   @Override  
  8.   public void handle(ActionEvent event)  
  9.   {  
  10.     System.out.println("yahoo");  
  11.   }  
  12. }  
  13. Using Lambda expressions:  
  14. 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 give us the path to java.util.stream. These streams are different from the regular streams. There is an 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 of class. Then we had java.sql.The 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 the local date, local timezone date-time, year, month, day, offset time 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.