Oracle 公司于 2014 年 3 月 18 日发布 Java 8 ;这个版本至今统治 Java 开发,成为开发的主力 Java 版本。

显然,Java 11 / 17 已经有赶上 Java 8 的占比;但,Java 8 在开发届依然有 30% - 50% 的占比。

随着,Spring AI 对Java版本最低要求为 Java 19 ,Java 8 的使用率肯定会进一步降低。

但,很多人依然无法熟练掌握 Java 8 的语法。

https://docs.oracle.com/javase/8/docs/

What's New in JDK 8

Java Programming Language
  • Lambda Expressions, a new language feature, has been introduced in this release. They enable you to treat functionality as a method argument, or code as data. Lambda expressions let you express instances of single-method interfaces (referred to as functional interfaces) more compactly.
  • Method references provide easy-to-read lambda expressions for methods that already have a name.
  • Default methods enable new functionality to be added to the interfaces of libraries and ensure binary compatibility with code written for older versions of those interfaces.
  • Repeating Annotations provide the ability to apply the same annotation type more than once to the same declaration or type use.
  • Type Annotations provide the ability to apply an annotation anywhere a type is used, not just on a declaration. Used with a pluggable type system, this feature enables improved type checking of your code.
  • Improved type inference.
  • Method parameter reflection.

方法引用

// 声明一个类
 
@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());
    }
}

构造器

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

静态方法

cars.forEach( Car::collide );

特定类的任意对象

cars.forEach( Car::repair );

特定对象的方法

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

Base64

内嵌类

序号 内嵌类 & 描述
1 static class Base64.Decoder

该类实现一个解码器用于,使用 Base64 编码来解码字节数据。

2 static class Base64.Encoder

该类实现一个编码器,使用 Base64 编码来编码字节数据。

方法

序号 方法名 & 描述
1 static Base64.Decoder getDecoder()

返回一个 Base64.Decoder ,解码使用基本型 base64 编码方案。

2 static Base64.Encoder getEncoder()

返回一个 Base64.Encoder ,编码使用基本型 base64 编码方案。

3 static Base64.Decoder getMimeDecoder()

返回一个 Base64.Decoder ,解码使用 MIME 型 base64 编码方案。

4

static Base64.Encoder getMimeEncoder()

返回一个 Base64.Encoder ,编码使用 MIME 型 base64 编码方案。

5 static Base64.Encoder getMimeEncoder(int lineLength, byte[] lineSeparator)

返回一个 Base64.Encoder ,编码使用 MIME 型 base64 编码方案,可以通过参数指定每行的长度及行的分隔符。

6 static Base64.Decoder getUrlDecoder()

返回一个 Base64.Decoder ,解码使用 URL 和文件名安全型 base64 编码方案。

7 static Base64.Encoder getUrlEncoder()

返回一个 Base64.Encoder ,编码使用 URL 和文件名安全型 base64 编码方案。

Lambda表达式

(parameters) -> expression
或
(parameters) ->{ statements; }
// 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)

函数式接口 Functional Interface 

@FunctionalInterface
interface GreetingService 
{
    void sayMessage(String message);
}

JDK 1.8 之前已有的函数式接口:

  1. java.lang.Runnable
  2. java.util.concurrent.Callable
  3. java.security.PrivilegedAction
  4. java.util.Comparator
  5. java.io.FileFilter
  6. java.nio.file.PathMatcher
  7. java.lang.reflect.InvocationHandler
  8. java.beans.PropertyChangeListener
  9. java.awt.event.ActionListener
  10. javax.swing.event.ChangeListener

JDK 1.8 新增加的函数接口:

        java.util.function

参 考

《Java8 指南》中文翻译 | JavaGuide

Java 8 新特性 | 菜鸟教程

Java SE Development Kit 8 Documentation

—— Java Development Kit 8 Documentation

The Java® Language Specification 1.8 版本

Oracle Java 1.8 用户手册 

——   The Java® Language Specification

——   Chapter 19. Syntax 这是语法部分

Oracle Java FX

——  Oracle JavaFX

Java FX  —— JavaFX is an open source, next generation client application platform for desktop, mobile and embedded systems built on Java.

JavaFX    And   Main - Main - OpenJDK Wiki

Oracle Java 21

JDK 21 Documentation - Home

Oracle Java 17

JDK 17 Documentation - Home

Oracle Java 19

JDK 19 Documentation - Home

Oracle Java 11

JDK 11 Documentation - Home

Oracle Java 24

JDK 24 Documentation - Home


Java Development Kit 8 Update Release Notes

JDK 8 Update Release Notes

The Java Language Environment: Contents
A White Paper

The Java Language Environment: Contents

Oracle JDK Docs List

JDK Release Notes

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐