Saturday, 10 September 2016

Whats new in Java 8?

Background

In this post well see what all new features and changes have come in Java 8 release.


Whats new?

  • Default methods are introduced in Java 8 which means you can provide a method with body in your interface and all concrete classes need not implement it. They can override it though. For this you need to use default keyword in your method. More details - 
  • Java 8 has also introduced Lambda expressions which use functional interface. You can see more details below - 
  • As you know for local variables to be accessed by methods in anonymous classes the local variable needs to be declared final. However from Java 8 it is accessible even if it is effectively final. More details - 
  • As we know variables in an interface are implicitly public, final and static and methods we public and abstract. Though variables remain the same with default methods we saw in point no 1 , non abstract methods are also possible in interface now. Also static methods are allowed in interface now. Following code snippet works from Java 8 -

    public interface TestInterface {
        
        String NAME = "Aniket";    //public static final 
        String getName();    //public abstract
        
        default String getDefaultName() { // non static default method
            return "Abhijit";
        }
        
        static String getNonDefaultSttaicName() { // static methods
            return NAME;
        }
    
    }
    
  • Changes in HashMap : The performance has been improved by using balanced trees instead of linked lists under specific circumstances. It has only been implemented in the classes -
    • java.util.HashMap,
    • java.util.LinkedHashMap and 
    • java.util.concurrent.ConcurrentHashMap.

      This will improve the worst case performance from O(n) to O(log n).
  • Java 8 introduces another new syntax called method references. Covered it a new post -
  • Java 8 also introduces a new class called Optional which is a better way to represent values that may not be present instead of using null and and adding null checks -
  • Lastly another Major change that was added was the Stream APIs. You can read all about it in following post -
  • Walking a directory using Streams API in java. This is a continuation of previous post NIO.2 API directory traversal using Path and FileVisitor which was introduced in java 7.
  • In java 8 new APIs are added for Date and Time. 


Related Links

No comments:

Post a Comment

t> UA-39527780-1 back to top