How does returning an || statement work in java?

前端 未结 7 931
北荒
北荒 2021-01-29 16:04

I\'m new to programming so I have been working through the exercises at CodingBat.com. In the Recursion-2 section, there is a problem I wasn\'t able to solve (recursively) so I

7条回答
  •  忘掉有多难
    2021-01-29 16:41

    return a || b;
    

    does not mean:

    return a || return b;
    

    It actually means:

    return (a || b);
    

    It will evaluate a || b, and then return the result of that:

    So if a or b is true (either one, or both, are true), it will return true. It will only be false if a and b are both false.

    The point is: it evaluates the boolean expression first, then returns the value.


    Edit: You can look at this test code for examples on how it works.

提交回复
热议问题