Are there good uses for non-short-circuiting logical (boolean) operators in Java/Scala?

后端 未结 4 1624
栀梦
栀梦 2020-12-07 00:42

I recently discovered that Java (and Scala) include non-short-circuiting logical operators &, |, and ^. I previously thought thes

4条回答
  •  心在旅途
    2020-12-07 01:42

    If you are trying to track answers or input for something, and that depends on both sides of your non-short-circuit boolean running.

    As an example, say you have:

    if(methodA() & methodB()){
        //some code
    }
    

    And within the methodB() method some essential code is running. If that was a short circuit code ( && ) and methodA() was false, methodB() would never run.

    That is one of the uses I can think of, at least.

提交回复
热议问题