“Missing return statement” within if / for / while

后端 未结 7 1122
粉色の甜心
粉色の甜心 2020-11-22 02:02

I have a question regarding return statements used within if() while() or for() statements. As you can see in the following method, it

7条回答
  •  猫巷女王i
    2020-11-22 02:08

    You have to add a return statement if the condition is false.

    public String myMethod() {
        if(condition) {
            return x;
        }
    
    //  if condition is false you HAVE TO return a string
    //  you could return a string, a empty string or null
        return otherCondition;  
    }
    

    FYI:

    Oracle docs for return statement

提交回复
热议问题