Блог пользователя eatmore

Автор eatmore, история, 4 года назад, По-английски

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.

  • Проголосовать: нравится
  • +77
  • Проголосовать: не нравится

»
4 года назад, # |
  Проголосовать: нравится +9 Проголосовать: не нравится

Has anyone any benchmarks regarding execution speed of various java release, especially java 8 vs other releases of java?