abacabadabacaba's blog

By abacabadabacaba, history, 4 weeks ago, In English,

Java 13 has been released a few days ago. Here are some changes that may be useful for competitive programming.

  • Switch expressions: first introduced in Java 12. In Java 13, a value is returned from a switch expression using yield statement:
String numType = switch (num) {
	0 -> "zero";
	1 -> "one";
	default -> {
		for (int i = 2; i < num; i++) {
			if (num % i == 0) {
				yield "composite";
			}
		}
		yield "prime";
	}
};
  • Text blocks: multi-line string literals, similar to Python:
String input = """
	3 3
	1 2
	1 3
	2 3""";

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

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

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

»
4 weeks ago, # |
  Vote: I like it -47 Vote: I do not like it

Java is very underrated for cp . I hope no one will downvote this .

»
4 weeks ago, # |
Rev. 2   Vote: I like it +1 Vote: I do not like it

Thanks. How exactly it works for leading spaces in lines?

String input = """
	3 3
	1 2
	1 3
	2 3""";

I mean that it seems the result is `3 3\n1 2\n1 3\n2 3'. Is any auto-trim policy processing multi-line string literals?

  • »
    »
    4 weeks ago, # ^ |
    Rev. 3   Vote: I like it 0 Vote: I do not like it

    "It's complicated", see https://openjdk.java.net/jeps/355 Basically, the "common white space prefix" and all trailing whitespace is dropped. Example:

    String html = """
    ..............<html>...
    ..............    <body>
    ..............        <p>Hello, world</p>....
    ..............    </body>.
    ..............</html>...
    ..............""";
    
    Whitespace, which is represented by the dots, would be removed.
»
4 weeks ago, # |
  Vote: I like it +5 Vote: I do not like it

Why not just Kotlin?