Java 14
Java 14 新特性介绍
在
Switch Expressions
在
新的
var log = switch (event) {
case PLAY -> "User has triggered the play button";
case STOP, PAUSE -> "User needs a break";
default -> {
String message = event.toString();
LocalDateTime now = LocalDateTime.now();
yield "Unknown event " + message +
" logged on " + now;
}
};
Text Blocks
String html = "<HTML>" +
"\n\t" + "<BODY>" +
"\n\t\t" + "<H1>\"Java 14 is here!\"</H1>" +
"\n\t" + "</BODY>" +
"\n" + "</HTML>";
使用文本块,您可以简化此过程并使用界定文本块开头和结尾的三个引号来编写更优雅的代码:
String html = """
<HTML>
<BODY>
<H1>"Java 14 is here!"</H1>
</BODY>
</HTML>""";
与普通的字符串文字相比,文本块还提供了更高的表达能力。
String literal =
"Lorem ipsum dolor sit amet, consectetur adipiscing " +
"elit, sed do eiusmod tempor incididunt ut labore " +
"et dolore magna aliqua.";
使用文本块中的
String text = """
Lorem ipsum dolor sit amet, consectetur adipiscing \
elit, sed do eiusmod tempor incididunt ut labore \
et dolore magna aliqua.\
""";
Pattern Matching for instanceof
if (obj instanceof Group) {
Group group = (Group) obj;
// use group specific methods
var entries = group.getEntries();
}
可以改写为如下:
if (obj instanceof Group group) {
var entries = group.getEntries();
}
由于条件检查断言
@Override public boolean equals(Object o) {
return (o instanceof CaseInsensitiveString) &&
((CaseInsensitiveString) o).s.equalsIgnoreCase(s);
}
通过删除对
@Override public boolean equals(Object o) {
return (o instanceof CaseInsensitiveString cis) &&
cis.s.equalsIgnoreCase(s);
}
这是一个有趣的预览功能,因为它为更广泛的模式匹配打开了大门。模式匹配的思想是为语言功能提供方便的语法,以根据某些条件提取对象的成分。
换句话说,此预览功能仅仅是个开始,您可以期待一种语言功能,它可以帮助进一步减少冗长性,从而减少错误的可能性。
Records
该版本中还有另一种预览语言功能:记录。像到目前为止提出的其他想法一样,此功能遵循减少
为了直接解决问题,请使用简单的域类
- The constructor
- Getter methods
- toString()
- hashCode() and equals()
此类组件的代码通常由
public class BankTransaction {
private final LocalDate date;
private final double amount;
private final String description;
public BankTransaction(final LocalDate date,
final double amount,
final String description) {
this.date = date;
this.amount = amount;
this.description = description;
}
public LocalDate date() {
return date;
}
public double amount() {
return amount;
}
public String description() {
return description;
}
@Override
public String toString() {
return "BankTransaction{" +
"date=" + date +
", amount=" + amount +
", description='" + description + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
BankTransaction that = (BankTransaction) o;
return Double.compare(that.amount, amount) == 0 &&
date.equals(that.date) &&
description.equals(that.description);
}
@Override
public int hashCode() {
return Objects.hash(date, amount, description);
}
}
public record BankTransaction(Date date,
double amount,
String description) {}
使用记录,您可以“自动”获取除构造函数和获取方法外的
javac --enable-preview --release 14 BankTransaction.java
记录的字段是隐式最终的。这意味着您无法重新分配它们。请注意,但这并不意味着整个记录都是不变的。存储在字段中的对象本身可以是可变的。
Helpful NullPointerExceptions
有人说抛出
var name = user.getLocation().getCity().getName();
在
Exception in thread "main" java.lang.NullPointerException
at NullPointerExample.main(NullPointerExample.java:5)
不幸的是,如果在第getLocation()
和 getCity()
,两者都可能返回<strong> NullPointerException </strong>
。
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Location.getCity()" because the return value of "User.getLocation()" is null
at NullPointerExample.main(NullPointerExample.java:5)