eatmore's blog

By eatmore, history, 5 years ago, In English

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.

  • Vote: I like it
  • +77
  • Vote: I do not like it

»
5 years ago, # |
  Vote: I like it +9 Vote: I do not like it

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