JEP 364: ZGC 垃圾回收機制
zgc垃圾回收機制con很早以前就聽說了這個概念,從jdk14開始zgc進入預覽版本,也意味着jdk15後,zgc將成爲java運行環境的默認垃圾回收器。
開啓ZGC方法
編譯的過程增加如下的選項
–with-jvm-features=zgc
在java參數文件,添加如下參數
-XX:+UnlockExperimentalVMOptions -XX:+UseZGC
http://openjdk.java.net/jeps/364
http://openjdk.java.net/jeps/365
JEP 363: 移除 併發標記修改(CMS)垃圾回收器
http://openjdk.java.net/jeps/363
再通過-XX:+UseConcMarkSweepGC使用CMS 垃圾回收器將會發生如下錯誤:
Java HotSpot™ 64-Bit Server VM warning: Ignoring option UseConcMarkSweepGC;
support was removed in
JEP 368: 文本塊 二次預覽
http://openjdk.java.net/jeps/368
這個特性比較簡單,針對長文本,SQL,在舊版本的處理方式是
String html = "<html>\n" +
" <body>\n" +
" <p>Hello, world</p>\n" +
" </body>\n" +
"</html>\n";
顯然用+的方式處理顯得比較的冗餘
新版本助攻採用的是如下方式,通過3個雙引號括起來,之前jdk13是採用,但是在後臺果斷放棄掉了,所以,這裏建議大型開發團隊在更新jdk的時候,一定要審慎,避免出現這種尷尬的情況
String html=`
<html>
<body>
<p>Hello, world</p>
</body>
</html>
`;
String html = """
<html>
<body>
<p>Hello, world</p>
</body>
</html>
""";
那麼問題來了,如果需要拼接一些變量應該怎麼辦呢,我們記得,在Groovy或者python中是可以類似如下的場景的
username='jjj'
age=18
str='this student:$username,age:$age'
這樣直接替換的,一來簡明易懂,但是java在這方面處理比較麻煩,但是還是有方法下面提供2個方法
方法1
String code = """
public void print(""" + type + """
o) {
System.out.println(Objects.toString(o));
}
""";
方法2
String code = """
public void print($type o) {
System.out.println(Objects.toString(o));
}
""".replace("$type", type);
方法3
String code = String.format("""
public void print(%s o) {
System.out.println(Objects.toString(o));
}
""", type);
方法4
String source = """
public void print(%s object) {
System.out.println(Objects.toString(object));
}
""".formatted(type);
怎麼樣,是不是很麻煩,筆者也是這麼覺得的。
另外java還提供3個方法,有興趣的讀者自行研究
String::stripIndent(): used to strip away incidental white space from the text block content
String::translateEscapes(): used to translate escape sequences
String::formatted(Object… args): simplify value substitution in the text block
JEP 305: instanceof 的擴展
//傳統的instanceof嚐嚐會 是如下的幾個步驟
//1.判斷是否爲目標類型
//2.如果是,轉化爲對應目標類型
//3.處理
Object obj="123";
if(obj instanceof String){
String s= (String) obj;
System.out.println(s);
}else{
System.out.println("unexpected type of object");
}
新版本的處理方式可以如下,instanceof類型後加一個變量,然後,在括號裏的代碼快
if(obj instanceof String s){
System.out.println(s);
}else{
System.out.println("unexpected type of object");
}
http://openjdk.java.net/jeps/305
JEP 358: 擴展空指針異常詳細
http://openjdk.java.net/jeps/358
JEP 359: 新數據類型Records(預覽特性)
http://openjdk.java.net/jeps/359
簡述
Records是一個新的概念,也是一個新的關鍵字
他的作用是簡化程序員書寫的代碼,針對Javabean 的書寫,功能類似於scala的case,kotlin的data。c#中的record
代碼
/**
* jdk14書寫方式只需要在括號中聲明javabean屬性
* 就會自動的幫我們生成對應名稱的屬性和方法,
* 以及toString,equals,方法
*/
record Range(int x,int y){
}
//編譯後的代碼
static final class Range extends java.lang.Record {
private final int x;
private final int y;
public Range(int x, int y) { /* compiled code */ }
public java.lang.String toString() { /* compiled code */ }
public final int hashCode() { /* compiled code */ }
public final boolean equals(java.lang.Object o) { /* compiled code */ }
public int x() { /* compiled code */ }
public int y() { /* compiled code */ }
}
//使用實例代碼
var r=new Range(7,8);
//自動提供了toString方法
System.out.println(r.toString());
//自動提供了equals方法
System.out.println(r.equals(new Range(8,7)));
//通過屬性獲取屬性
System.out.println(r.x+":"+r.x);
//通過方法獲取屬性
System.out.println(r.x()+":"+r.y());
//對字段的放射方法
Arrays.asList(r.getClass().getRecordComponents()).forEach(it->{
String name = it.getName();
System.out.println(name);
});
//用於通過反射判斷一個對象是否爲record類型的對象
System.out.println(r.getClass().isRecord());
//同樣的,他也是支持自己手動的去處理構造函數邏輯,代碼如下
record Range(int lo, int hi) {
public Range {
if (lo > hi) /* referring here to the implicit constructor parameters */
throw new IllegalArgumentException(String.format("(%d,%d)", lo, hi));
}
}