- Interface의 변화
- Java8 이전 interface는 public abstract methods만 허용했다. 하지만 Java8 이후부터 interface에서 static과 default 메서드를 사용할 수 있다.
1) static method
- static 메서드를 구현하여 사용
public interface People{ void work(); static String eat() { return "맛있다." } } String eating = People.eat();
2) Default Metohd
- default 메서드는 default 키워드를 선언하여 사용할 수 있다. 그냥 사용할 수도 있고 override하여 사용할 수도 있다.
public interface People{ void work(); default String eat() { return "맛있다." } } People eating = new People(); String eating = People.eat();
- 가장 큰 단점이었던 한번 배포된 인터페이스는 수정이 어려웠는데 위 두가지 메소드로 수정이 편리해졌다.
- 함수형 프로그래밍의 적용과 람다 스트림 등장
- 컬렉션 프레임워크를 개선하는데 바탕이 됐다.
Collections.sort(customers, new Comparator<Customer>() {
public int compare(Customer o1, Customer o2) {
//JAVA8이전
return o1.getName().compareTo(o2.getName());
}
});
customers.sort(comparing(Customer::getName)); //JAVA8 이후