Java 14 was released a few days ago. Here are some changes that may be useful for competitive programming.
- Switch expressions, first introduced in Java 12, are now standard:
var whatToDo = switch (outcome) {
"CE" -> "Check if you chose the right language";
"RE", "WA" -> "Check your logic";
"TL", "ML" -> "Check your complexity";
"AC" -> "Celebrate";
default -> "???";
};
- Pattern matching for
instanceof
:
public boolean equals(Object o) {
return (o instanceof Point p) && x == p.x && y == p.y;
}
- Helpful
NullPointerException
s: now they include a message indicating which particular value wasnull
. - Records:
record Fraction(int num, int den) {
Fraction {
if (den == 0) {
throw new IllegalArgumentException("zero denominator");
}
if (den < 0) {
num = -num;
den = -den;
}
}
Fraction add(Fraction o) {
return new Fraction(num * o.den + den * o.num, den * o.den);
}
// ...
}
PrintStream.write(byte[])
andPrintStream.writeBytes(byte[])
may be used for fast output, but they are just shorthands forwrite(byte[], int, int)
.
If you know about other useful features, post it here.