Java 14 for competitive programming

Правка en1, от eatmore, 2020-03-20 21:24:17

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 NullPointerExceptions: now they include a message indicating which particular value was null.
  • 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[]) and PrintStream.writeBytes(byte[]) may be used for fast output, but they are just shorthands for write(byte[], int, int).

If you know about other useful features, post it here.

Previous posts: Java 8, 9, 10, 11, 12 and 13.

Теги java, java 14

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en1 Английский eatmore 2020-03-20 21:24:17 1415 Initial revision (published)