Java 8 released on March 18, 2014 which provided lots of new features. Some of them are:-
- Changes in Interface – default and static method
- Functional Interface
- Lambda Expression
- Method and Constructor References
- Stream API
- Date Time API
Need of Default and Static Methods in Interface?
As you know before Java 8, everytime we modify interface by putting some methods over there, we need to modify our implementation classes also. Think if you have 10 of implementation/concrete classes of an interface. Everytime you modify your interface need to modify the concrete classes as well.
To resolve this problem, Java introduces the concept of default and static methods by which you can provide method implementation within the interface. Read More
A functional interface is an interface that contains only one abstract method. You can define as many default and static methods within functional interface. The main reason to introduce functional interface is Lambda Expression. As in lambda expression we can provide the implementation of only one method.
@FunctionalInterface annotation is used to ensure that the functional interface can’t have more than one abstract method. If you define more than one abstract the compiler raises error for the same. Read More
A lambda expression is implementation of functional interface that is being implemented or instantiated anonymously. A lambda expression represents an anonymous function. Read More
Method Reference & Constructor Reference
A method reference is the shorthand syntax for a lambda expression. As you know through lamba expression we can implement functional interface having single method.
Consumer c = ()->System.out.println("Start Thread");
Using Method Reference
Consumer c = System.out::println;
Constructor Reference is used for passing constructor reference to lambda expression.
Supplier<String> producer = String::new;
The Java 8 Streams API concept is based on converting Collections to a Stream, processing the elements in parallel and then gathering the resulting elements into a Collection. Read More
Java 8 Date Time API
Java 8 introduced new APIs for Date and Time to fulfill the limitation of the older java.util.Date and java.util.Calendar. As earlier developer needed to write thread safety code, zone date time logic etc. Java 8 introduces the LocalDate,LocalTime and LocalDateTime classes having zone date time features too.
To get instance of LocalDateTime
LocalDateTime currentTime = LocalDateTime.now(); System.out.println("Current DateTime: " + currentTime);