Java 8 (1.8)的语法更新,这么写才是现代 Java 编程代码写法 , Oracle 11 17 19 21 24 的 Docs
Java 8自2014年发布以来一直是主流开发版本,虽然Java 11/17占比上升,但仍有30%-50%项目在使用。该版本引入了Lambda表达式、函数式接口、方法引用等核心特性,极大提升了开发效率。随着Spring AI等新技术要求Java 19+,Java 8使用率将逐步降低,但其语法特性仍是开发者必备技能。
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/
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 之前已有的函数式接口:
- java.lang.Runnable
- java.util.concurrent.Callable
- java.security.PrivilegedAction
- java.util.Comparator
- java.io.FileFilter
- java.nio.file.PathMatcher
- java.lang.reflect.InvocationHandler
- java.beans.PropertyChangeListener
- java.awt.event.ActionListener
- javax.swing.event.ChangeListener
JDK 1.8 新增加的函数接口:
java.util.function
参 考
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
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
Oracle Java 17
Oracle Java 19
Oracle Java 11
Oracle Java 24
Java Development Kit 8 Update Release Notes
The Java Language Environment: Contents
A White Paper
The Java Language Environment: Contents
Oracle JDK Docs List
更多推荐
所有评论(0)