關於 Java12 的一切——新特性、安全性和 Switch 表達式語句(示例)
已發表: 2019-04-07 19th March 2019
,Java12 發布。 眾所周知, Java12
是快速發布的一部分,它在 Java11 發布後僅 6 個月就發布了。
在本教程中,我們將介紹有關 Java12 的所有更改和新功能。
就我個人而言,我的所有開發都切換到了 Java12,但對於生產週期來說還為時過早。 如果您想為所有生產項目切換到 Java12,請將本教程添加為書籤。
Java12 有什麼新功能? Java12 的新特性:
Java12 中有很多內部和用戶工作流相關的特性發生了變化。 讓我們來看看 Java 12 裡面有什麼。
Change-1) 並發類卸載
普通垃圾收集器通常會在 GC 循環期間卸載未使用的變量,並且我們通常會注意到進程中的一些停止/暫停,或者在此期間 CPU 增加。 通常我們甚至不會注意到這一點。
使用ZGC
(Z Garbage Collector) – Java12 也支持並發類卸載。 由於這發生在正常的 GC 週期中,因此沒有任何暫停,也沒有更多的內存額外使用。
默認情況下,Java12 中啟用了 ZGC。 無需更多操作
如何禁用 ZGC?
- 只需使用 JVM 命令行參數
-XX:-ClassUnloading
啟動您的應用程序
Change-2) 獲取有關 JVM Crash 的更多詳細信息
當出現 OOM(Out Of Memory)錯誤或 JVM 崩潰時,Java 通常會創建包含所有詳細信息的轉儲文件。
1 |
- XX : HeapDumpPath =/ tmp / crunchify / - XX : + HeapDumpOnOutOfMemoryError |
使用此 JVM 參數,將在 OOM 錯誤時在/tmp/crunchify/
文件夾下創建轉儲文件。
Java12 中又增加了一個選項:
1 |
- XX : + ExtensiveErrorReports |
將創建名為hs_err<pid>.log
的新日誌文件,其中包含有關 JVM 崩潰的所有詳細信息。 如果您看到頻繁的崩潰並想要進行更多調試,這對您的生產環境非常有幫助。
默認情況下它是禁用的,但您可以通過添加上面的 JVM 命令行參數來啟用廣泛的崩潰報告。
Change-3) 緊湊數字格式
java.text 添加了對緊湊數字格式的支持。 100o
可以稱為1K
, 100000
可以稱為100K
。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package crunchify . com . tutorials ; import java . text . NumberFormat ; import java . util . Locale ; /** * @author Crunchify.com * Java12 Compact Number format example * */ public class CrunchifyJava12CompactNumber { public static void main ( String args [ ] ) { // NumberFormat is the abstract base class for all number formats. // This class provides the interface for formatting and parsing numbers. NumberFormat also provides methods for determining which locales have number formats, and what their names are. NumberFormat crunchifyFormat = NumberFormat . getCompactNumberInstance ( Locale . US , NumberFormat . Style . SHORT ) ; // getCompactNumberInstance returns a compact number format for the specified locale and formatStyle. String crunchifyResult = crunchifyFormat . format ( 100000 ) ; System . out . println ( "NumberFormat.Style.SHORT Result: " + crunchifyResult ) ; } } |
結果:
1 |
NumberFormat . Style . SHORT Result : 100K |
Change-4) Java 安全性增強
security-libs/java.security
更改:
- 禁止和允許 java.security.manager 的選項
- 如果
disallow
,則 System.setSecurityManager
不能用於設置安全管理器。
- 如果
- -groupname 選項添加到 keytool 密鑰對生成
- 用戶可以在生成密鑰對時指定一個命名組。
- 自定義
PKCS12 keystore
生成- 包括算法和參數
- 密鑰保護
- 證書保護
- 麥克數據
- 包括算法和參數
- 新的 JFR 安全事件
- 什麼是 JFR(Java 飛行記錄器)
- 添加了 4 個新的 JFR 事件
- jdk.X509證書
- jdk.X509驗證
- jdk.TLS握手
- jdk.SecurityPropertyModification
Change-5) JEP 325:切換錶達式
Java12 現在支持增強型Switch statement
。
- 基於 Java 12 的
case L -> syntax
操作。 這裡沒有任何休息的必要。 - 開關表達式的使用
- 這是簡化的 switch 語句
- 如果標籤匹配,則僅執行箭頭標籤右側的表達式。
- 不需要中斷語句。
CrunchifyJava12SwitchExample.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
package crunchify . com . tutorials ; import java . util . Scanner ; /** * @author Crunchify.com * What's new in Java12 Switch statement? * */ public class CrunchifyJava12SwitchExample { public static void main ( String [ ] args ) { Scanner crunchifyObj = new Scanner ( System . in ) ; log ( "Enter company name from: Google, Facebook, PayPal, eBay, Twitter, LinkedIn, Apple" ) ; String company = crunchifyObj . nextLine ( ) ; log ( "Selected Company: " + company ) ; // Pre-Java12 Switch statement switch ( company ) { case "Google" : case "Facebook" : case "PayPal" : case "eBay" : case "Twitter" : log ( "Pre-Java12: This switch is for companies Google, Facebook, PayPal, eBay & Twitter" ) ; break ; case "" : case "Apple" : case "LinkedIn" : log ( "Pre-Java12: This switch is for companies Apple & LinkedIn" ) ; break ; default : log ( "Pre-Java12: Oops... Invalid company" ) ; } /** * Java 12 based case L -> syntax operation. * Here there isn't any break necessary. */ switch ( company ) { case "Google" , "Facebook" , "PayPal" , "eBay" , "Twitter" - > log ( "Java12: This switch is for companies Google, Facebook, PayPal, eBay & Twitter" ) ; case "Apple" , "LinkedIn" - > log ( "Java12: This switch is for companies Apple & LinkedIn" ) ; default - > { log ( "Java12: Oops... Invalid company" ) ; } } /** * This is switch expression */ final String companyName ; companyName = switch ( company ) { case "Google" , "Facebook" , "PayPal" , "eBay" , "Twitter" - > ( "Java12 Expression: This switch is for companies Google, Facebook, PayPal, eBay & Twitter" ) ; case "Apple" , "LinkedIn" - > ( "Java12 Expression: This switch is for companies Apple & LinkedIn" ) ; /** * it's also possible to do switch operation without a block and break */ default - > { break "Java12 Expression: Oops... Invalid company" ; } } ; log ( companyName ) ; } public static void log ( String result ) { System . out . println ( result ) ; } } |

IntelliJ IDEA 結果:
1 2 3 4 5 6 7 8 |
Enter company name from : Google , Facebook , PayPal , eBay , Twitter , LinkedIn , Apple Twitter Selected Company : Twitter Pre - Java12 : This switch is for companies Google , Facebook , PayPal , eBay & Twitter Java12 : This switch is for companies Google , Facebook , PayPal , eBay & Twitter Java12 Expression : This switch is for companies Google , Facebook , PayPal , eBay & Twitter |
Change-6) JVM 常量 API
java.lang.invoke.constant
:您可能知道,Java 類有一個常量池,它在運行時存儲所有操作數。
Java12 添加了用於在運行時調用常量的 API。
從 Java12 中刪除的功能:
Java12 中已棄用的功能:
如果您想在此處包含有關 Java12 的任何方便的教程,請告訴我。