jeudi 3 novembre 2016

Why Am I Missing A Return Statement With Else

I'm trying to understand why my compiler is complaining that I'm missing a return statement, when in fact I have fail-safe else at the end of my method. Why is this the case? When would the else not cover a return?

Here's a primitive example:

public boolean greaterOrEqual(int a){
    int z = 10;
    if(z > a){
       return false;
    }
    else if(z < a){
       return true;
    }
    else{
       return true; //if if and else if fail, else is the last resort
    }
}

Functionally, it would be the same as this

public boolean greaterOrEqual(int a){
    int z = 10;
    if(z > a){
       return false;
    }
    else if(z < a){
       return true;
    }

return true; //if if and else if fail, returns true

}

Aucun commentaire:

Enregistrer un commentaire