Switch expression with string concatenation

Tagir Valeev amaembo at gmail.com
Tue Nov 20 09:40:28 UTC 2018


Seems that switch expression behaves badly with string concatenation. Example:

public class Java12Test {
  public static void main(String[] args) {
    String s = "foo";
    System.out.println(s + switch(0) {
      default -> {
        s = "bar";
        break 1;

>"C:\Program Files\Java\jdk-12\bin\java.exe" -version
openjdk version "12-ea" 2019-03-19
OpenJDK Runtime Environment (build 12-ea+20)
OpenJDK 64-Bit Server VM (build 12-ea+20, mixed mode, sharing)

>"C:\Program Files\Java\jdk-12\bin\javac.exe" --enable-preview --release=12 Java12Test.java
Note: Java12Test.java uses preview language features.
Note: Recompile with -Xlint:preview for details.

>"C:\Program Files\Java\jdk-12\bin\java.exe" --enable-preview Java12Test

As you can see, `s = "bar"` assignment was not executed.

With best regards,
Tagir Valeev

More information about the compiler-dev mailing list