一文梳理Java?8后的新功能
為什么要寫這篇文章
經(jīng)過了若干年的發(fā)展,Java逐步從java8升級為java11,java17。
讓我們對比學(xué)習(xí)一下最新一版的LTS版本和java8比起來讓代碼簡化了多少。
文本塊(Text Blocks)
這個(gè)寫法有些類似于 javascript、 Lua等腳本語言。方便識別html、json等格式復(fù)雜的字符串。
public class StringTest { public static void main(String[] args) throws Exception { // 傳統(tǒng)寫法 String json = "{\n" + " \"key\":\"a\",\n" + " \"value\":\"b\"\n" + "}"; // 優(yōu)化后寫法 String json2 = """ { "key":"a", "value":"b" } """; // 返回為 true System.out.println(json == json2); } }
本地變量類型推斷(Local Variable Type Inference)
這一點(diǎn)也是在一些腳本語言中常見的,類似于 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); // 循環(huán)語句 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 表達(dá)式 意思相同 BiFunction<Integer, Integer, Integer> biFunction = (a, b) -> a + b; BiFunction<Integer, Integer, Integer> biFunction2 = (var a, var b) -> a + b; }
switch
public static void main(String[] args) throws Exception { var eating = Eating.BREAKFAST; String eatingZnString = ""; // 傳統(tǒng)寫法 switch (eating) { case BREAKFAST: case LUNCH: eatingZnString = "早午飯"; break; case DINNER: eatingZnString = "晚飯"; break; default: throw new Exception(); } System.out.println(eatingZnString); // 優(yōu)化后寫法 System.out.println( switch (eating) { case BREAKFAST,LUNCH -> "早午飯"; case DINNER -> "晚飯"; default -> throw new Exception(); } ); }
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 { // 傳統(tǒng)寫法 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("沒有這種動物的叫聲"); } } // 優(yōu)化寫法 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("沒有這種動物的叫聲"); } } }
record 類
// 傳統(tǒng)類 public final class People { 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() {...} } // 優(yōu)化后的類 public record People (String name, int age){ } // 更多用法 public record People (String name, int age){ // 靜態(tài)字段 static int teenageAge; // 靜態(tài)初始化 static { teenageAge = 17; } // 靜態(tài)方法 public static People buildTeenage(String name) { return new People(name , teenageAge); } // 優(yōu)化后的構(gòu)造方法 public People { if (age < 0) { throw new IllegalArgumentException("年齡不能小于0"); } } }
參考文檔
總結(jié)
到此這篇關(guān)于Java 8后新功能的文章就介紹到這了,更多相關(guān)Java8后的新功能內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談Java隨機(jī)數(shù)的原理、偽隨機(jī)和優(yōu)化
這篇文章主要介紹了淺談Java隨機(jī)數(shù)的原理、偽隨機(jī)和優(yōu)化,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01Java反射之通過反射獲取一個(gè)對象的方法信息(實(shí)例代碼)
下面小編就為大家?guī)硪黄狫ava反射之通過反射獲取一個(gè)對象的方法信息(實(shí)例代碼)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10Javascript和Java語言有什么關(guān)系?兩種語言間的異同比較
雖然Javascript與Java有緊密的聯(lián)系,但卻是兩個(gè)公司開發(fā)的不同的兩個(gè)產(chǎn)品。那么js和java有什么關(guān)系,兩種語言的不同點(diǎn)是什么呢?介于這兩個(gè)問題,小編一起給大家解答下2016-09-09