java8的新特性

1.Lambda表达式
lambda允许函数作为一个方法的参数。
特性:

  • 可选的类型声明:不需要声明参数类型,编译器可以统一识别。
  • 可选的参数圆括号():一个参数的时候无需定义(),多个参数需要定义();
  • 可选的大括号{}:如果主体包含了一个语句,就不需要使用大括号{};
  • 可选的返回关键字:如果主体只有一个表达式返回值则编译器会自动返回值。
    简单的例子:
// 1. 不需要参数,返回值为 5  
() -> 5  
// 2. 接收一个参数(数字类型),返回其2倍的值  
x -> 2 * x  
// 3. 接受2个参数(数字),并返回他们的差值  
(x, y) -> x – y  
// 4. 接收2个int型整数,返回他们的和  
(int x, int y) -> x + y  
// 5. 接受一个 string 对象,并在控制台打印,不返回任何值(看起来像是返回void)  
(String s) -> System.out.print(s)
public class Java8Tester {
   public static void main(String args[]){
      Java8Tester tester = new Java8Tester();
               
      // 类型声明
      MathOperation addition = (int a, int b) -> a + b;
               
      // 不用类型声明
      MathOperation subtraction = (a, b) -> a - b;
               
      // 大括号中的返回语句
      MathOperation multiplication = (int a, int b) -> { return a * b; };
               
      // 没有大括号及返回语句
      MathOperation division = (int a, int b) -> a / b;
               
      System.out.println("10 + 5 = " + tester.operate(10, 5, addition));
      System.out.println("10 - 5 = " + tester.operate(10, 5, subtraction));
      System.out.println("10 x 5 = " + tester.operate(10, 5, multiplication));
      System.out.println("10 / 5 = " + tester.operate(10, 5, division));
               
      // 不用括号
      GreetingService greetService1 = message ->
      System.out.println("Hello " + message);
               
      // 用括号
      GreetingService greetService2 = (message) ->
      System.out.println("Hello " + message);
               
      greetService1.sayMessage("Runoob");
      greetService2.sayMessage("Google");
   }
       
   interface MathOperation {
      int operation(int a, int b);
   }
       
   interface GreetingService {
      void sayMessage(String message);
   }
       
   private int operate(int a, int b, MathOperation mathOperation){
      return mathOperation.operation(a, b);
   }
}

返回结果:
10 + 5 = 15
10 - 5 = 5
10 x 5 = 50
10 / 5 = 2
Hello Runoob
Hello Google

2.方法引用
可以直接引用已有的java对象或对象的方法或构造器。
与lambda一起使用,可以使语言的构造更加紧凑简洁。
方法引用使用一对冒号 ::

下面,我们在 Car 类中定义了 4 个方法作为例子来区分 Java 中 4 种不同方法的引用。

package com.runoob.main;

@FunctionalInterface
public interface Supplier<T> {
    T get();
}

class Car {
    //Supplier是jdk1.8的接口,这里和lamda一起使用了
    public static Car create(final Supplier<Car> supplier) {
        return supplier.get();
    }

    public static void collide(final Car car) {
        System.out.println("Collided " + car.toString());
    }

    public void follow(final Car another) {
        System.out.println("Following the " + another.toString());
    }

    public void repair() {
        System.out.println("Repaired " + this.toString());
    }
}

构造器引用:它的语法是Class::new,或者更一般的Class< T >::new实例如下:

final Car car = Car.create( Car::new );
final List< Car > cars = Arrays.asList( car );

静态方法引用:它的语法是Class::static_method,实例如下:

cars.forEach( Car::collide );

特定类的任意对象的方法引用:它的语法是Class::method实例如下:

cars.forEach( Car::repair );

特定对象的方法引用:它的语法是instance::method实例如下:

final Car police = Car.create( Car::new );
cars.forEach( police::follow );

3.默认方法
接口中可以有实现方法。
默认方法就是接口可以有实现方法,而且不需要实现类去实现其方法。
我们只需在方法名前面加个 default 关键字即可实现默认方法。

public class Java8Tester {
   public static void main(String args[]){
      Vehicle vehicle = new Car();
      vehicle.print();
   }
}

interface Vehicle {
   default void print(){
      System.out.println("我是一辆车!");
   }
       
   static void blowHorn(){
      System.out.println("按喇叭!!!");
   }
}

interface FourWheeler {
   default void print(){
      System.out.println("我是一辆四轮车!");
   }
}

class Car implements Vehicle, FourWheeler {
   public void print(){
      Vehicle.super.print();
      FourWheeler.super.print();
      Vehicle.blowHorn();
      System.out.println("我是一辆汽车!");
   }
}
我是一辆车!
我是一辆四轮车!
按喇叭!!!
我是一辆汽车!

4.新工具
新的编译工具,如Nashorn引擎,jjs、类依赖分析器jdeps。
https://www.runoob.com/java/java8-nashorn-javascript.html

5.Stream API
将真正的函数式编程风格引入到java中。
https://www.runoob.com/java/java8-streams.html
6.Optional 类
Optional 类已经成为 Java 8 类库的一部分,用来解决空指针异常
https://www.runoob.com/java/java8-optional-class.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值