Why am I getting an AssertionError when assigning Arrays.asList() to var directly?

后端 未结 2 1106
傲寒
傲寒 2021-02-12 03:34

I\'m trying to understand local variable type inference in Java 10.

  1. The code below works perfectly during compilation and runtime:

    List list1 =         
    
    
            
相关标签:
2条回答
  • 2021-02-12 03:51

    This is a bug in Java 10 compiler: https://bugs.openjdk.java.net/browse/JDK-8199910

    It is only reproduced when javac is called with a -g flag.

    Possible workarounds:

    1. Do not use the -g flag
      • If you use IDEA: Settings → Build, Execution, Deployment → Compiler → Java Compiler → Uncheck "Generate Debugging Info"
    2. Do not use var with intersection types (use explicit types):
      • List<Object> list = Arrays.asList(1L, 2.0F, "3");
      • var list = Arrays.<Object> asList(1L, 2.0F, "3");
    3. Use Eclipse which has its own compiler

    UDPATE:

    The bug was fixed in JDK 10.0.2.

    0 讨论(0)
  • 2021-02-12 04:07

    This is a bug of openjdk, see this:

    Javac should skip non-denotable types in the LocalVariableTypeTable attribute

    0 讨论(0)
提交回复
热议问题