LoginSignup
1
1

More than 3 years have passed since last update.

IntelliJ IDEAでjava 13のswitchをためす

Posted at

※ 2019/11/15 時点です。

IntelliJ IDEAまったくと言っていいほど触ったことが無いので、自分用にメモ。

環境

IntelliJ IDEA 2019.2.4 (Community Edition)

手順

New ProjectでProject SDKに13を選択する。

intelljeideaswith0001.jpg

https://twitter.com/java/status/1193898422849269760http://blog.codefx.org/java/switch-expressions/ を参考にサンプルコードを書く。

import java.io.FileNotFoundException;

public class Main {

    static enum MY_BOOLEAN {
        TRUE, FALSE, FILE_NOT_FOUND,
    }

    public static void main(String[] args) throws Exception {
        MY_BOOLEAN ternaryBool = MY_BOOLEAN.TRUE;

        boolean result = switch (ternaryBool) {
            case TRUE -> true;
            case FALSE -> false;
            case FILE_NOT_FOUND -> throw new RuntimeException("This is ridiculous!", new FileNotFoundException());
            // as we'll see in "Exhaustiveness", `default` is not necessary
            default -> throw new IllegalArgumentException("Seriously?! 🤬");
        };
        System.out.println(result);

    }
}

実行すると下記のようなエラーになる。

Information:java: Errors occurred while compiling module 'untitled'
Information:javac 13.0.1 was used to compile java sources
Information:2019/11/15 16:13 - Build completed with 1 error and 0 warnings in 3 s 286 ms
Error:java: invalid source release 12 with --enable-preview
  (preview language features are only supported for release 13)

Open Module Settings -> Project Settings -> Modules -> Language level を13 (Preview)に変更する。

intelljeideaswith0002.jpg

これで動く。language levelを変えてあげれば--enable-previewは無くても動くらしい。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1