老板放过我吧!我Java8还没用呢,又让我了解Java14
阅读本文大概需要 7 分钟。
改进的switch表达式,次出现在Java 12和13中,在Java 14中获得了完全的支持
instanceof支持模式匹配(语言特性)
NullPointerException(JVM特性)
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 = "<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.\
""";
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);
}
关注微信公众号「程序员的成长之路」后台回复「2048」关键字,免费获取5T技术学习资源!包括但不限于:C/C++,Linux,Python,Java,PHP,人工智能,单片机,树莓派,等等。
构造器
getter方法
toString()
hashCode()和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) == &&
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
-XX:+ShowCodeDetailsInExceptionMessages 下面是个例子:
java -XX:+ShowCodeDetailsInExceptionMessages NullPointerExample
推荐阅读:
今天跟我薅当当羊毛!4 折买正版书了!!
对一次钓鱼事件的分析
微信扫描二维码,关注我的公众号
朕已阅
相关文章