Following are some of the key features introduced in Java 8
1. Lambda Expressions: Functional programming constructs introduced in Java programming language to simplify java code.
2. Method references: Feature that enables reference to methods and method constructors without executing them.
3. Default and Static methods in Interfaces: Enables to add new functionality to interfaces, without impacting implementing classes.
4. Repeating annotations: Enables to repeat the same annotation multiple times to a declaration or type.
5. Type annotations: Annotations that can be used anywhere where a type is used.
6. Collections - Streams API: Functional-style operations that act on streams of data elements.
7. Security: New features and enhancements to Java Security API.
8. Concurrency: New interfaces and classes added to Concurrency API.
Lambda expressions is a Java programming language feature introduced in Java 8 that provides functional programming constructs to the Java programming language, which simplifies the Java code in certain cases such as with Java anonymous inner classes. Lambda expressions blends functional programming features with object oriented programming features of Java resulting in simplified and more powerful concurrency features.
Lambda expressions are blocks of Java code that can be defined and passed as data which can be executed at a later time.
Prior to Java 8, Interfaces could contain only method declarations in the form of abstract methods.
In Java 8, interfaces can contain method implementations in the form of static methods and default methods.
Static methods, similar to the class static methods, can now be implemented in interfaces. Interface static methods cannot be overridden by implementing classes. Similar to a class static method, an interface static method can be invoked outside of the interface by using interface name.
Default methods are declared using the 'default' keyword, and can be added either to new interfaces or to existing interfaces. New classes implementing the interface can override the default methods. Existing classes already implementing the interface are not impacted.
//interface with abstract, default and static methods
public class MyInterface() {
//abstract method
abstract void logAbstract();
//default method
default void logDefault() {
System.out.println('default method invoked');
}
//static method
static void logStatic() {
System.out.println('static method invoked');
}
}
Java 8.0 introduced a new Java language feature regarding annotations. You can now repeat an annotation multiple times on a type declaration.
Java 8.0 introduced a new Java language feature regarding annotations. In addition to using annotations on type declarations, you can now apply annotations whenever you use types.
Following are some examples of Type annotations
@NonNull
@ReadOnly
@Regex,
@Tainted
@Untainted