java unreachable code不可達代碼
例子:
static int test(int testval) {
int target = 1;
if (testval > target)
System.out.println(1);
return -1;(下邊是可達永遠不會達到的代碼,所以編譯器提示錯誤(unreachable code))
if (testval < target)
return +1;
return 0; // match
}、代碼
修改為讓return -1 與上邊的可達if為一體的就行,修改如下:
static int test(int testval) {
int target = 1;
if (testval > target)
return -1;
if (testval < target)
return +1;
return 0; // match
}
或者這樣直接輸出:
static int test(int testval) {
int target = 1;
if (testval > target)
System.out.println(1);
if (testval < target)
return +1;
return 0; // match
}
注:始終遵循著if if 條件判斷原則,代碼上邊那個滿足就不在執行下邊的可達代碼
public boolean check() { String str = null; for (int i = 0; i < 1; i++) for (int j = 0; j < 1; j++) if (str == null) return true; return false; }
System.out.println(new Type().check()); true
作者:love_you_girl
來源鏈接:https://www.cnblogs.com/love-you-girl/p/3901631.html