为什么要写这篇文章

经过了若干年的发展,Java逐步从java8升级为java11java17

让我们对比学习一下最新一版的LTS版本和java8比起来让代码简化了多少。

  1. 文本块(Text Blocks)。

这个写法有些类似于 javascript、 Lua等脚本语言。方便识别html、json等格式复杂的字符串。

public class StringTest {
public static void main(String[] args) throws Exception {
// 传统写法
String json =
"{\n" +
" \"key\":\"a\",\n" +
" \"value\":\"b\"\n" +
"}";
// 优化后写法
String json2 = """
{
"key":"a",
"value":"b"
}
""";
// 返回为 true
System.out.println(json == json2);
}
}
  1. 本地变量类型推断(Local Variable Type Inference)

这一点也是在一些脚本语言中常见的,类似于 var 表示变量,val 表示常量。

    public static void main(String[] args) throws Exception {
//集合
// immutable map build
var map = Map.of(
"cat", "猫",
"dog", "狗",
"fish", "鱼");
// immutable set build
var set = Set.of("1", "2", "3");
// immutable list build
var list = List.of(1, 2, 3, 4, 5); // 循环语句
for (var i = 1; i < list.size(); i++) {
System.out.println(i);
}
for (var i : list) {
System.out.println(i);
} // 异常
try (var in = new ByteArrayInputStream("123".getBytes())) {
System.out.println(new String(in.readAllBytes(), "utf-8"));
} catch (Exception e) {
System.out.println(e);
} // lambda 表达式 意思相同
BiFunction<Integer, Integer, Integer> biFunction = (a, b) -> a + b;
BiFunction<Integer, Integer, Integer> biFunction2 = (var a, var b) -> a + b;
}
  1. switch
    public static void main(String[] args) throws Exception {
var eating = Eating.BREAKFAST;
String eatingZnString = "";
// 传统写法
switch (eating) {
case BREAKFAST:
case LUNCH:
eatingZnString = "早午饭";
break;
case DINNER:
eatingZnString = "晚饭";
break;
default:
throw new Exception();
}
System.out.println(eatingZnString); // 优化后写法
System.out.println(
switch (eating) {
case BREAKFAST,LUNCH -> "早午饭";
case DINNER -> "晚饭";
default -> throw new Exception();
}
);
}
  1. instance of操作符的模式匹配(Pattern Matching for the instanceof Operator)

interface Animal {} class Cat implements Animal {
public void mew() {
System.out.println("喵");
}
} class Dog implements Animal {
public void woof() {
System.out.println("汪");
}
} public class Test {
// 传统写法
public static void sounds(Animal animal) {
if (animal instanceof Cat) {
Cat cat = (Cat) animal;
cat.mew();
} else if (animal instanceof Dog) {
Dog dog = (Dog) animal;
dog.woof();
} else {
throw new IllegalArgumentException("没有这种动物的叫声");
}
} // 优化写法
public static void betterSounds(Animal animal) {
if (animal instanceof Cat cat) {
cat.mew();
} else if (animal instanceof Dog dog) {
dog.woof();
} else {
throw new IllegalArgumentException("没有这种动物的叫声");
}
}
}
  1. record 类
// 传统类
public record People(String name, int age) {
public People(String name, int age) {
this.name = name;
this.age = age;
} public String name() {
return this.name;
} public int age() {
return this.age;
}
public boolean equals(People people) {...}
public int hashCode() {...}
public String toString() {...}
} // 优化后的类
public record People (String name, int age){ } // 更多用法
public record People (String name, int age){
// 静态字段
static int teenageAge; // 静态初始化
static {
teenageAge = 17;
}
// 静态方法
public static People buildTeenage(String name) {
return new People(name , teenageAge);
} // 优化后的构造方法
public People {
if (age < 0) {
throw new IllegalArgumentException("年龄不能小于0");
}
}
}

参考文档

Java Language Updates

Java 8 后的新功能梳理的更多相关文章

  1. Java 17 将要发布,补一下 Java 13 中的新功能

    本文章属于Java 新特性教程 系列,已经收录在 Github.com/niumoo/JavaNotes ,点个赞,不迷路. 自从 Oracle 调整了 Java 的版本发布节奏之后,Java 版本发 ...

  2. Java 14 新功能介绍

    不做标题党,认认真真写个文章. 文章已经收录在 Github.com/niumoo/JavaNotes 和未读代码博客,点关注,不迷路. Java 14 早在 2019 年 9 月就已经发布,虽然不是 ...

  3. Java 16 新功能介绍

    点赞再看,动力无限.Hello world : ) 微信搜「程序猿阿朗 」. 本文 Github.com/niumoo/JavaNotes 和 程序猿阿朗博客 已经收录,有很多知识点和系列文章. Ja ...

  4. Java 17 新功能介绍(LTS)

    点赞再看,动力无限.Hello world : ) 微信搜「程序猿阿朗 」. 本文 Github.com/niumoo/JavaNotes 和 未读代码博客 已经收录,有很多知识点和系列文章. Jav ...

  5. Java 11 新功能来了!

    关键时刻,第一时间送达! 目前 Oracle 已经发布了 Java Development Kit 10,下个版本 JDK 11 也即将发布.本文介绍 Java 11 的新功能. 根据Oracle新出 ...

  6. Java 11新功能抢先了解

    目前 Oracle 已经发布了 Java Development Kit 10,下个版本 JDK 11 也即将发布.本文介绍 Java 11 的新功能. 根据Oracle新出台的每6个月发布一次Jav ...

  7. 面试题思考:Java 8 / Java 7 为我们提供了什么新功能

    Java 7 的7个新特性 1.对集合类的语言支持: 2.自动资源管理: 3.改进的通用实例创建类型推断: 4.数字字面量下划线支持: 5.switch中使用string: 6.二进制字面量: 7.简 ...

  8. 超详细 Java 15 新功能介绍

    点赞再看,动力无限.微信搜「程序猿阿朗 」,认认真真写文章. 本文 Github.com/niumoo/JavaNotes 和 未读代码博客 已经收录,有很多知识点和系列文章. Java 15 在 2 ...

  9. Java 19 新功能介绍

    点赞再看,动力无限. 微信搜「程序猿阿朗 」. 本文 Github.com/niumoo/JavaNotes 和 未读代码博客 已经收录,有很多知识点和系列文章. Java 19 在2022 年 9 ...

随机推荐

  1. miRNA分析--靶基因预测(三)

    miRNA分析--数据过滤(一) miRNA分析--比对(二) 根据miRNA Target Prediction in Plants, miRNA并非所有区域都要求严格匹配,其中第1位碱基和第14位 ...

  2. C语言 指针数组指针

    指向指针数组的指针. 1 #include <stdio.h> 2 3 int main(void) 4 { 5 char *pa[4]={"aaaaa"," ...

  3. ggplot2 图例及分页参数

    图例: 1 theme(legend.title =element_blank()) 2 guides(fill = guide_legend(title = NULL)) # 去掉图例title 3 ...

  4. SQL-join(inner join)、left join、right join、full join

    0.JOIN 类型 有时我们需要从两个或更多的表中获取结果,数据库中的表可通过键将彼此联系起来.每个表中都有一个主键,主键(Primary Key)是一个列,值都唯一.这样做的目的是在不重复每个表中的 ...

  5. phpexcel 另存Excel文件方式

    $w = new PHPExcel_Writer_Excel5($e); $dir = 'path/title.xls'; $w->save($dir);

  6. Tikz绘制形似万花尺的图片

    初中时意外发现数学课本上有这么一个好玩的图 大概就是把两条相等线段A.B分为10个小段并在点上标序号,A线段1点连B线段9点,2点连8点,依次类推. 假设有这么一个框架图 按照第一张图的方式进一步绘图 ...

  7. 分布式事务(4)---最终一致性方案之TCC

    分布式事务(1)-理论基础 分布式事务(2)---强一致性分布式事务解决方案 分布式事务(3)---强一致性分布式事务Atomikos实战 强一致性分布式事务解决方案要求参与事务的各个节点的数据时刻保 ...

  8. 基于阿里云ecs(centos 7) 安装jenkins

    1. 安装好 jdk 2. 官网(https://pkg.jenkins.io/redhat-stable/)下载rpm包(稳定版): wget https://pkg.jenkins.io/redh ...

  9. JS - 获取当前的时间,并且转换成年 - 月 - 日格式!

    先获取当前时间,并转换成年月日格式! function getNowFormatDate() { var date = new Date(); var seperator1 = "-&quo ...

  10. JavaEE复习二

    Servlet应用开发接口: javax.servlet.Servlet: init()方法:调用在构造方法之后,在service方法之前: service()方法:调用此方法允许Servlet响应请 ...