Java 8中的三元运算符,使用Maven编译

时间:2021-09-10 22:30:01

Consider this class:

考虑这个课程:

package be.duo.test;

public class Main {

    public static void main(String[] args) {
        Main main = new Main();
        main.execute();
    }

    private void execute() {
        for(int i = 0; i < 10; i++) {
            System.out.println("" + method());
        }
    }

    private int method() {
        return (Math.random() > 0.5d) ? 1 : null;
    }

}

The method() has return type int, which is a primitive type.

method()的返回类型为int,它是一种基本类型。

Consider the ternary operator used in the return statement:

考虑return语句中使用的三元运算符:

  • it compiles with the Java 8 default compiler, but this will result in a NullPointerException at runtime, why?
  • 它用Java 8默认编译器编译,但这会在运行时导致NullPointerException,为什么?

  • using Maven this will result in a compile time error
  • 使用Maven会导致编译时错误

[ERROR] error: incompatible types: bad type in conditional expression
[ERROR] <null> cannot be converted to int

Can somebody explain to me why it behaves different?

有人可以向我解释为什么它表现不同吗?

2 个解决方案

#1


As far as I can tell, it should be legal under Java 8.

据我所知,它应该在Java 8下合法。

See Table 15.25-E. Conditional expression type (Reference 3rd operand, Part III):

见表15.25-E。条件表达式类型(参考第3操作数,第III部分):

3rd → null
2nd ↓        
int   lub(Integer,null)

lub(Integer,null) should be Integer. Basically if you have a conditional of the form boolean ? int : null, the result of the expression should be Integer and it gets unboxed. (I think you already know this is what happens.)

lub(整数,null)应该是Integer。基本上如果你有一个布尔形式的条件? int:null,表达式的结果应该是Integer并且它被取消装箱。 (我想你已经知道这是发生了什么。)

So according to the specification it should be the same.

所以根据规范它应该是相同的。

Seems like a compiler bug. There have been quite a few of these found, I would say try updating to the newest version.

看起来像编译器错误。已经发现了不少这些,我会说尝试更新到最新版本。

#2


Not sure which Java 8 minor version you use, but i can compile under java 1.8.

不确定您使用的是哪个Java 8次要版本,但我可以在java 1.8下编译。

C:\Users\XXXX>javac -version
javac 1.8.0_31

C:\Users\XXXX>javac Main.java

C:\Users\XXXX>java Main
Exception in thread "main" java.lang.NullPointerException
        at Main.method(Main.java:15)
        at Main.execute(Main.java:10)
        at Main.main(Main.java:5)

#1


As far as I can tell, it should be legal under Java 8.

据我所知,它应该在Java 8下合法。

See Table 15.25-E. Conditional expression type (Reference 3rd operand, Part III):

见表15.25-E。条件表达式类型(参考第3操作数,第III部分):

3rd → null
2nd ↓        
int   lub(Integer,null)

lub(Integer,null) should be Integer. Basically if you have a conditional of the form boolean ? int : null, the result of the expression should be Integer and it gets unboxed. (I think you already know this is what happens.)

lub(整数,null)应该是Integer。基本上如果你有一个布尔形式的条件? int:null,表达式的结果应该是Integer并且它被取消装箱。 (我想你已经知道这是发生了什么。)

So according to the specification it should be the same.

所以根据规范它应该是相同的。

Seems like a compiler bug. There have been quite a few of these found, I would say try updating to the newest version.

看起来像编译器错误。已经发现了不少这些,我会说尝试更新到最新版本。

#2


Not sure which Java 8 minor version you use, but i can compile under java 1.8.

不确定您使用的是哪个Java 8次要版本,但我可以在java 1.8下编译。

C:\Users\XXXX>javac -version
javac 1.8.0_31

C:\Users\XXXX>javac Main.java

C:\Users\XXXX>java Main
Exception in thread "main" java.lang.NullPointerException
        at Main.method(Main.java:15)
        at Main.execute(Main.java:10)
        at Main.main(Main.java:5)