What is wrong with the short circuit logic in this Java code?

后端 未结 9 1096
挽巷
挽巷 2021-01-18 01:58

Why doesn\'t func3 get executed in the program below? After func1, func2 doesn\'t need to get evaluated but for func3, shouldn\'t it?

if (func1() || func2()          


        
9条回答
  •  鱼传尺愫
    2021-01-18 02:23

    Java short-circuits boolean expressions. That means that, once func1() is executed and returns true, the rest of that boolean doesn't matter since you are using an or operator. No matter what func2() && func3() evaluates to, the whole expression will evaluate to true. Thus, Java doesn't even bother evaluating the func2() or func3().

提交回复
热议问题