欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

一文梳理Java?8后的新功能

 更新時(shí)間:2022年02月08日 14:57:42   作者:Kwanwooo  
Java 8是Java自Java 5(發(fā)布于2004年)之后的最重要的版本,下面這篇文章主要給大家介紹了關(guān)于Java8后新功能的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下

為什么要寫這篇文章

經(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");
        }
    }
}

參考文檔

Java Language Updates

總結(jié)

到此這篇關(guān)于Java 8后新功能的文章就介紹到這了,更多相關(guān)Java8后的新功能內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論