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 -
- Using Lambda Expressions from Java 8
- Using Lambda expressions from Java 8 - Part 2
- Common Functional interfaces introduced in Java 8
- 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.
- Collection improvements in Java 8
Related Links
- Whats new in Java7?(OSFG)
- Understanding Default methods in Java 8(OSFG)
- Using Lambda Expressions from Java 8(OSFG)
- Using Lambda expressions from Java 8 - Part 2(OSFG)
- Common Functional interfaces introduced in Java 8(OSFG)
- Use of final local variables in Java(OSFG)
- Java 8 Method references and Lambda expression(OSFG)
- Understanding and using Java 8 Optional class(OSFG)
- Understanding Java 8 Stream API(OSFG)
- Java 8 Stream terminal operations - reduce vrs collect(OSFG)
- Walking a directory using Streams API in java8(OSFG)
- Java File I/O (NIO.2) (OSFG)
- Working with Java 8 Date/Time API (OSFG)
- Java 8 Collection improvements (OSFG)
No comments:
Post a Comment