if 引数が true の場合に、if else ケースで Java プログラムがエラーにならないのはなぜですか。なぜ例外を作らないのですか?たとえば、ここで 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は厳密なコンパイルをサポートしていませんか? この問題の背後にある原則は何ですか?