Java 14 新增 5 项新特性, 我还在用Java 8
开发者技术前线
共 7364字,需浏览 15分钟
·
2020-08-22 01:17
点击“开发者技术前线”,选择“星标?”
在看|星标|留言, 真爱
Java 14预定于3月17日发布。那么,14版究竟有什么新功能,对于整天写代码、维护代码的Java开发者来说,哪些功能最有用呢?
以下为译文:
改进的switch表达式,第一次出现在Java 12和13中,在Java 14中获得了完全的支持
instanceof支持模式匹配(语言特性)
NullPointerException(JVM特性)
希望你在阅读完本文后,积极地代码中实验这些功能,为Java团队提供反馈,并为Java的发展做出贡献。
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;
}
};
String html = "" +
"\n\t" + "" +
"\n\t\t" + "
\"Java 14 is here!\"
" +"\n\t" + "" +
"\n" + "";
String html = """
"Java 14 is here!"
""";
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.\
""";
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);
}
构造器
getter方法
toString()
hashCode()和equals()
这些部分的代码通常由IDE自动生成,而且会占用很大篇幅。下面是生成的完整的BankTransaction类:
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(LocalDate date,
double amount,
String description) {}
javac --enable-preview --release 14 BankTransaction.java
var name = user.getLocation().getCity().getName();
Exception in thread "main" java.lang.NullPointerException
at NullPointerExample.main(NullPointerExample.java:5)
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)
后果:Location.getCity()无法被调用
原因:User.getLocation()的返回值为null
增强版本的诊断信息只有在使用下述标志运行Java时才有效:
-XX:+ShowCodeDetailsInExceptionMessages
java -XX:+ShowCodeDetailsInExceptionMessages NullPointerExample
扫码下面二维码,在后台回复关键字:Google规范,可获取完整 pdf 版《Google Java编程风格规范》。
扫码加我微信进群,大厂内推和技术交流,大佬们零距离
开发者技术前线 ,汇集技术前线快讯和关注行业趋势,大厂干货,是开发者经历和成长的优秀指南。
评论