且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

局部变量的Java 10类型推断可以推断出无效吗?

更新时间:2023-09-15 23:24:34

为什么你认为它编译?它不编译:

Why do you think it compiles? It doesn't compile:

> javac Main.java
Main.java:5: error: cannot infer type for local variable v2
        var v2 = emptyMethod(); // no problem at all
            ^
  (variable initializer is 'void')
1 error

您可能使用IntelliJ IDEA,对吗? IDEA目前没有检测到这种错误。有一个错误: https://youtrack.jetbrains.com/issue/IDEA-188623

You probably use IntelliJ IDEA, do you? IDEA currently does not detect such kind of an error. There is a bug for that: https://youtrack.jetbrains.com/issue/IDEA-188623