当 if 参数为真时,为什么 java 程序在 if else 情况下不会出错。为什么它没有任何例外。例如,这里的method1和method2即使有无法访问的语句也不会产生任何(编译)错误,但是method3会产生编译错误。首先仔细阅读代码并提供答案。
public int method1() {
if(true) {
return 1;
} else {
return 2;//unreachable statement but doesn't make exception
}
}
public int method2() {
if(true) {
return 1;
} else if (true) {
return 2;//unreachable statement but doesn't make exception
} else {
return 3;//unreachable statement but doesn't make exception
}
}
public int method3() {
if(true) {
return 1;
} else if (true) {
return 2;//unreachable statement but doesn't make exception
} else {
return 3;//unreachable statement but doesn't make exception
}
return 3;//unreachable statement but makes exception
}
java不支持严格编译吗?这个问题背后的原理是什么?