Consider this class, compiled with Maven:
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.
Consider the short IF notation in the return statement:
- in Java 6, this will result in a NullPointerException at runtime
- in Java 8, this will result in a compile time error
[ERROR] error: incompatible types: bad type in conditional expression
[ERROR] <null> cannot be converted to int
My source code is thus not forward compatible. Can somebody explain to me why it behaves different?
Aucun commentaire:
Enregistrer un commentaire