你可能未曾使用的新 Java 特性
Java 是在過去 20 年中一直在市場流行的編程語言。
但是最近幾年各種替代 Java 的聲音不斷。
與 Python,Kotlin,Swift 等現(xiàn)代編程語言相比,開發(fā)人員抱怨 Java 過時的編程語法。
但是很多人不知道的是,Java 新版做了很多改進,并為開發(fā)人員提供了更有效的方式來編寫代碼。
如果您想用 Java 更輕松編寫簡潔和優(yōu)雅的代碼,可以參考以下一些建議,這些語法在 JDK 14 已經(jīng)提供。
1 Try-with-resource 語句
使用 try-catch 塊處理異常,通常需要通過 finally 塊來添加清除代碼?,F(xiàn)在使用 try with resource 語法,開發(fā)人員就不用自己操心資源釋放。
我們可以在 try 括號內(nèi)添加資源,以便在 try-catch 塊執(zhí)行后關(guān)閉或清理該資源
舊語法
- Scanner scanner = null;
- try {
- scanner = new Scanner(new File("foo.txt"));
- while (scanner.hasNext()) {
- System.out.println(scanner.nextLine());
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } finally {
- if (scanner != null) scanner.close();
- }
新語法
- try (Scanner scanner = new Scanner(new File("foo.txt"))) {
- while (scanner.hasNext()) {
- System.out.println(scanner.nextLine());
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
這將大大減少關(guān)閉流或數(shù)據(jù)庫連接的代碼行,并減少由于忘記關(guān)閉流/連接而導(dǎo)致各種錯誤的問題。
2 switch 表達式
開發(fā)人員經(jīng)常遇到需要從條件塊返回值的情況,但是舊的語法不好解決。
舊語法
- private String getUserRole(User user){
- String userRole = "";
- switch(user.getRole()){
- case 0:
- userRole = "Customer";
- break;
- case 1:
- userRole = "Editor";
- break;
- case 2:
- userRole = "Admin";
- break;
- default: throw new IllegalStateException("Unexpected value: " + user.getRole());
- }
- return userRole;
- }
就像 Swift 這些現(xiàn)代語言一樣,Java 12 引入了 switch 表達式,你可以根據(jù)條件返回值。
新語法
- private String getUserRoleV2(User user){
- return switch(user.getRole()){
- case 0 -> "Customer";
- case 1 -> "Editor";
- case 2 :
- // for multi line expression use 'yield' keyword
- user.setRights(AuthRights.absolute);
- yield "Admin";
- default -> throw new IllegalStateException("Unexpected value: " + user.getRole());
- };
- }
這大大減少了項目中的 LOC(代碼行),并使修改相對容易。
3 用 var 初始化
Java 本質(zhì)上是嚴格類型的語言,使用嚴格類型定義是開發(fā)人員偏好的問題。但是支持類型推斷可以降低代碼復(fù)雜性,Java 10 增加了對局部變量的類型推斷的支持。
- private void init(){
- var str = "Java 10"; // infers String
- var list = new ArrayList<String>();
- var stream = list.stream(); // infers Stream<String>
- }
但 Java 仍然是一種靜態(tài)類型的語言,僅在有足夠的信息可用于初始化時才推斷類型。因此如果變量滿足以下條件,則使用 var 初始化是合法的:
- 它只能是局部變量(類成員或函數(shù)參數(shù)不支持)
- 聲明后應(yīng)立即定義(define)
4 記錄 (record)
使用 Java 最常見的抱怨之一,需要編寫大量代碼來使類可用,例如一堆 toString 或 equals 定義,因此代碼看起來很冗長。Java 14 提供了 Record 語法,使類型聲明更加簡潔,當我們需要在一個類名下綁定多個值時,它非常有用。
這是 Oracle 網(wǎng)站上的一篇文章示例,展示了使用記錄的優(yōu)勢
- var order = new FXOrderClassic(1,
- CurrencyPair.GBPUSD,
- Side.Bid, 1.25,
- LocalDateTime.now(),
- 1000);
像這樣的標準對象的調(diào)用,需要定義類型 FXOrderClassic。
舊語法
- public final class FXOrderClassic {
- private final int units;
- private final CurrencyPair pair;
- private final Side side;
- private final double price;
- private final LocalDateTime sentAt;
- private final int ttl;
- public FXOrderClassic(int units,
- CurrencyPair pair,
- Side side,
- double price,
- LocalDateTime sentAt,
- int ttl) {
- this.units = units;
- this.pair = pair; // CurrencyPair is a simple enum
- this.side = side; // Side is a simple enum
- this.price = price;
- this.sentAt = sentAt;
- this.ttl = ttl;
- }
- public int units() {
- return units;
- }
- public CurrencyPair pair() {
- return pair;
- }
- public Side side() {
- return side;
- }
- public double price() { return price; }
- public LocalDateTime sentAt() {
- return sentAt;
- }
- public int ttl() {
- return ttl;
- }
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass())
- return false;
- FXOrderClassic that = (FXOrderClassic) o;
- if (units != that.units) return false;
- if (Double.compare(that.price, price) != 0)
- return false;
- if (ttl != that.ttl) return false;
- if (pair != that.pair) return false;
- if (side != that.side) return false;
- return sentAt != null ?
- sentAt.equals(that.sentAt) : that.sentAt == null;
- }
- @Override
- public int hashCode() {
- int result;
- long temp;
- result = units;
- result = 31 * result +
- (pair != null ? pair.hashCode() : 0);
- result = 31 * result +
- (side != null ? side.hashCode() : 0);
- temp = Double.doubleToLongBits(price);
- result = 31 * result +
- (int) (temp ^ (temp >>> 32));
- result = 31 * result +
- (sentAt != null ? sentAt.hashCode() : 0);
- result = 31 * result + ttl;
- return result;
- }
- @Override
- public String toString() {
- return "FXOrderClassic{" +
- "units=" + units +
- ", pair=" + pair +
- ", side=" + side +
- ", price=" + price +
- ", sentAt=" + sentAt +
- ", ttl=" + ttl +
- '}';
- }
- }
新語法
- public record FXOrder(int units,
- CurrencyPair pair,
- Side side,
- double price,
- LocalDateTime sentAt,
- int ttl) {}
5 增強的 instance of
Java 14 引入了 instanceof 模式匹配的功能,這意味著在使用 instanceof 的實例類型檢查時,不再需要顯式的類型轉(zhuǎn)換。
舊語法
- private Entries getEntries(User user){
- if (user instanceof Editor) {
- Editor editor = (Editor) user;
- // use editor specific methods
- var entries = editor.getEntries();
- return entries;
- }
- return null;
- }
新語法
- private Entries getEntries(User user){
- if (user instanceof Editor editor) {
- // use group specific methods
- var entries = editor.getEntries();
- return entries;
- }
- return null;
- }
6 文本塊
支持文本塊并不是什么新鮮事,但在 Java 中卻是工程師期待已久的功能。Java 開發(fā)人員總是渴望以更簡單的方式打印多行字符串文字,而不使用討厭的串聯(lián)。Java 新版支持多行字符串文字。
舊語法
- String html = "<HTML>" +
- "\n\t" + "<BODY>" +
- "\n\t\t" + "<H2>\"Hurray! Java 14 is here\"</H2>" +
- "\n\t" + "</BODY>" +
- "\n" + "</HTML>";
使用三引號 ,就可以使用此功能,該功能在使用結(jié)構(gòu)化字符串時非常方便,例如以一定的對齊方式和間距編寫文件或添加多行 html 塊
新語法
- String html = """
- <HTML>
- <BODY>
- <H2>"Hurray! Java 14 is here"</H2>
- </BODY>
- </HTML>
- """;
7 有意義的 NPE (空指針)消息
空指針異常(NPE)一直是 Java 開發(fā)人員的噩夢,并且是開發(fā)人員遇到的最常見的問題。
但是 NPE 消息常常不能提供有關(guān)該問題的足夠信息。
- var task = new Task();
- final boolean isDataConsumed = task.getData().getBucket().isConsumed;
在代碼段中,可能存在多個故障點,例如
- getData()返回一個空值
- getBucket()返回一個空值
但以下 NPE 報錯未提供有關(guān)該問題的足夠詳細信息。
為了解決此問題,Oracle 添加 JEP 358,提供有用的 NullPointExceptions
NullPointerException 通過精確描述哪個變量來提高 JVM 生成錯誤信息的可用性。
你可以通過在 Java 命令中添加以下標志來使用此功能
-XX:+ShowCodeDetailsInExceptionMessages
使用該標志,JVM 將提供更多有意義的信息,以便跟蹤確切的故障點
英文原文:
https://medium.com/swlh/working-with-new-generation-of-java-236e2dc38316
本文轉(zhuǎn)載自微信公眾號「高可用架構(gòu)」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系高可用架構(gòu)公眾號。