一  引言

  现在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方法的签名来选择合适的构造函数

最新文章

  1. Mybatis入门DEMO
  2. Xcode 中的相对路径与绝对路径的相关设置
  3. wamp出现You don’t have permission to access/on this server提示的解决方法
  4. Squid代理服务器
  5. 【转】Oracle RAC 环境下的连接管理
  6. 关于javascript在作用域中的变量定义你所不知道的一些东西
  7. 现在流行什么 JS库/框架?
  8. android studio的里的 content_XXX_xml问题
  9. hibernate之.hbm.xml文件内容相关参数说明
  10. PHP学习笔记二
  11. J - A + B Problem II(第二季水)
  12. JavaScript语言基础知识10
  13. Angular2.0的项目架构
  14. 初学python笔记---列表
  15. win7&amp;win10 右键添加 cmd
  16. Access查询时间段 .
  17. 高性能mysql-锁的调试
  18. MySQL按日、周、月统计数据
  19. Java数组的定义和使用
  20. 从客户端(......)中检测到有潜在危险的 Request.Form 值

热门文章

  1. SpringBoot之整合Mybatis(增,改,删)
  2. 第六届蓝桥杯java b组第8题
  3. JavaSE知识点总结(一)
  4. Spring Cloud Config 配置中心实践过程中,你需要了解这些细节!
  5. 体验Code::Blocks下的Windows GUI编程(32 bit and 64 bit)
  6. [Python] Python 学习记录(1)
  7. 报错com.neenbedankt.android-apt not found如何解决
  8. 【JavaScript】使用纯JS实现多张图片的懒加载(附源码)
  9. Tomcat源码分析二:先看看Tomcat的整体架构
  10. 分享一个移动端rem布局的适配mixin