Java 8 introduced several powerful features that significantly enhanced the language, making it more expressive and flexible. One of the most notable additions was **default methods** in interfaces. Prior to Java 8, interfaces could only contain abstract methods, but with the introduction of default methods, interfaces can now provide a default implementation for non-abstract methods. This allows developers to add new methods to existing interfaces without breaking existing implementations.
For example:
```java
public interface Formula {
void doSomething();
default void before() {
System.out.println("I am an extension method");
}
}
```
A class implementing this interface can choose to override the `before()` method or use the default implementation. Here's an example of how it works:
```java
public class FormulaService implements Formula {
@Override
public void doSomething() {
before();
System.out.println("I am the override method");
}
}
```
Another major feature introduced in Java 8 is **lambda expressions**, which are the foundation of functional programming in Java. Lambda expressions allow you to write concise and readable code by replacing anonymous classes with a simpler syntax.
Before Java 8, sorting a list required an anonymous class:
```java
Collections.sort(list, new Comparator() {
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
});
```
With Java 8, this can be simplified using a lambda expression:
```java
Collections.sort(list, (String o1, String o2) -> o2.compareTo(o1));
```
This makes the code more compact and easier to understand. You can even make it even more concise by omitting the type declarations:
```java
Collections.sort(list, (o1, o2) -> o2.compareTo(o1));
```
**Functional interfaces** are another key concept in Java 8. These are interfaces that have exactly one abstract method. The `@FunctionalInterface` annotation is used to enforce this rule. If you try to define more than one abstract method in such an interface, the compiler will throw an error.
Here’s an example of a functional interface:
```java
@FunctionalInterface
public interface Formula {
T convert(F var1);
}
```
You can then use a lambda expression to implement it:
```java
Formula function = (var1) -> Integer.valueOf(var1);
Integer var2 = function.convert("1000");
```
In addition to lambda expressions, Java 8 also introduced **method references**, which provide a way to refer to methods or constructors directly. Method references are a shorthand for lambda expressions that call a single method.
The general syntax for a method reference is: `ClassName::methodName`.
For example, you can reference a static method like this:
```java
Formula function = Integer::valueOf;
```
Or an instance method on a specific object:
```java
String[] array = {"Jianming", "Xueyou"};
Arrays.sort(array, String::compareTo);
```
You can also reference a constructor using `ClassName::new`. For example:
```java
interface PersonFactory
{
P create(String name);
}
PersonFactory Bosch Truck Starter,Bosch Truck engine,Bosch engine YIWU JINGHONG AUTO PARTS CO.,LTD , https://en.jhauto.ru