Java 15 for competitive programming

Revision en1, by eatmore, 2020-10-02 05:05:58

Java 15 was released a few days ago. Here are some changes that may be useful for competitive programming.

  • Pattern matching for instanceof, first introduced in Java 14, is still a preview.
  • Text blocks, first introduced in Java 13, are now final:
String input = """
	3 3
	1 2
	1 3
	2 3""";
  • Records, first introduced in Java 14, are now final:
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);
	}

	// ...
}
  • Useful new methods: CharSequence.isEmpty() (also inherited by String), String.stripIndent() (useful for text blocks), String.formatted(), Math.absExact().

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

Tags java, java 15

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English eatmore 2020-10-02 05:05:58 1096 Initial revision (published)