为什么要写这篇文章

经过了若干年的发展,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. expr判断文件名以固定格式结尾

    #!/bin/bash if expr "$1" : ".*\.sh" &>/dev/null then echo "okok" ...

  2. 59. Divide Two Integers

    Divide Two Integers My Submissions QuestionEditorial Solution Total Accepted: 66073 Total Submission ...

  3. gg=G

    1.代码格式化对齐 2.直接按下ESE模式下就可以来执行了

  4. 『与善仁』Appium基础 — 18、元素定位工具(二)

    目录 1.Appium Inspector介绍 2.Appium Inspector打开方式 3.Appium Inspector布局介绍 4.Appium Inspector工具的配置 5.Appi ...

  5. linux下的C++多线程

    原文链接:http://blog.csdn.net/lee1054908698/article/details/54633056 本随笔作为多线程笔记使用,内容完全照搬原博 多线程是多任务处理的一种特 ...

  6. ReactiveCocoa操作方法-重复

    retry重试      只要失败,就会重新执行创建信号中的block,直到成功. __block int i = 0; [[[RACSignal createSignal:^RACDisposabl ...

  7. SpringMVC(1):SpringMVC入门

    一,MVC 概述 MVC:模型,视图,控制器,是一种软件设计规范,本质是将业务逻辑,数据,显示,分离的方式来编写代码:前后端分离 Model:数据模型,提供要展示的数据,一般我们都会把这两个分离开来. ...

  8. hive 启动不成功,报错:hive 启动报 Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hadoop/mapred/MRVersi

    1. 现象:在任意位置输入 hive,准备启动 hive 时,报错: Exception in thread "main" java.lang.NoClassDefFoundErr ...

  9. Linux运维实战之磁盘分区、格式化及挂载(一)

    在网络系统中,磁盘和文件系统管理是两个非常基本.同时也是非常重要的管理任务,特别是文件系统管理,因为它与用户权限和整个网络系统的安全息息相关.本次博文的主题是关于Linux系统中磁盘分区.格式化及挂载 ...

  10. Git初始化及仓库创建和操作

    一.基本信息配置 1.全局配置用户名 git config --global user.name "YeHuan-byte" 2.全局配置邮箱 git config --globa ...