JDK1.8 新特性详解

一  引言

  现在java 10都已经出来了,而自己对java 8的一些新特性都不了解,很是惭愧,而且许多面试都有问到java8的新特性,借此博客好好学习这些新特性

二  新特性

  1 default关键字

  之前在面试的时候,问到接口和抽象类的区别,自己愚蠢回答一个接口不能写方法的实现,不出所料,接下来问了一个jdk 1.8的新特性的问题,说实话之前有了解过jdk1.8,但是项目中用的是低版本,所以不是很熟悉,接下让我们学习一下jdk 1.8新引入的关键字default,通过default修饰的方法,我们可以在接口写方法的实现

public interface Test01 {
    public  void test01();
    
    public default void test02(){
        System.out.println("我是新特性");
    }
    
}

这样做的意义为何?看一下官方给出的解释

The section Interfaces describes an example that involves manufacturers of computer-controlled cars who publish industry-standard interfaces that describe which methods can be invoked to operate their cars. What if those computer-controlled car manufacturers add new functionality, such as flight, to their cars? These manufacturers would need to specify new methods to enable other companies (such as electronic guidance instrument manufacturers) to adapt their software to flying cars. Where would these car manufacturers declare these new flight-related methods? If they add them to their original interfaces, then programmers who have implemented those interfaces would have to rewrite their implementations. If they add them as static methods, then programmers would regard them as utility methods, not as essential, core methods.

哈哈,看不懂吧,其实我也看不懂,我说一下大体意思,老项目中的一个接口已经有很多的实现类,如果想在这个接口添加的新的方法,而其他实现这个接口的实现类无疑要重写这个方法,对项目而言这是灾难的,当然我们可以定义一个静态的方法(static),实现类无需重写,但是static更多是使用方法,而非核心的方法,这是java的规范

  2 重头戏   Lambda表达式

  这里引入一个概念,函数式编程: (引网上的概念)函数式编程是一种抽象程度很高的编程范式,而且纯粹函数式编程语音编写的函数是没有变量的,因此对于一个函数只要输入明确,输出就是确定的,而且这种纯函数没有副作用的,而允许使用变量的函数,由于内部的变量的状态不确定,同样的输入,可能得到不同的结果,这种函数有副作用的

  jdk1.8新的特性lambda,这说明java尝试把函数式编程引入

List<String> names = Arrays.asList("aaa", "bbb", "ccc", "eee");
        //之前我们这样写
        Collections.sort(names, new Comparator<String>() {
            @Override
            public int compare(String a, String b) {
                return b.compareTo(a);
            }
        });
        //lambda
        Collections.sort(names, (String a, String b) -> {
            return b.compareTo(a);
        });
        //还可以简化
        Collections.sort(names, (String a, String b) -> b.compareTo(a));
        //继续
        Collections.sort(names, (a, b) -> b.compareTo(a));
        System.out.println(names);
    }

  引入lambda最大的感受就是大大的简化代码的开发,借鉴的像Scala,Python的等编程语言的特点,jdk1.8定义许多函数式接口,当然我们可以自定义函数式接口,详细后续讲解

  3  函数式接口

  函数式接口仅且只能包含一个抽象的方法的接口,每一个该类型的lambda的表达式都会匹配的这个抽象的方法上的,jdk1.8提供@FunctionalInterface注解来定义函数式接口

@FunctionalInterface
public interface Haha {
public int test(int a,int b);
}
public static void main(String[] args) {

        System.out.println(((Haha)(a,b) -> a+b).test(2,5));;

    }

  4 方法与构造函数引用

  jdk1.8提供一种方式通过::关键字来传递方法或者引用,下面通过::关键字引用静态方法

public interface A<T,F> {
    F convert(T t);
}

public static void main(String[] args) {
        
        A<String, Integer> t = Integer::valueOf;
        Integer i = t.convert("111");
        System.out.println(i);
    }

输出结果为: 111

  通过::关键字::引用对象的方法,类的静态方法或者类的构造,进而赋给接口的中的(唯一)方法

class Person {
    String firstName;
    String lastName;
    Person() {}
    Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
}

interface PersonFactory<P extends Person> {
    P create(String firstName, String lastName);
}

PersonFactory<Person> personFactory = Person::new;
Person person = personFactory.create("Peter", "Parker");

我们只需要使用 Person::new 来获取Person类构造函数的引用,Java编译器会自动根据PersonFactory.create方法的签名来选择合适的构造函数

猜你喜欢

转载自www.cnblogs.com/developerxiaofeng/p/9177568.html