Reason for the existence of non-short-circuit logical operators

后端 未结 6 1236
借酒劲吻你
借酒劲吻你 2020-11-28 08:16

When used with boolean operands, & and | become logical operators per Section 15.22.2 of the JLS. Unlike &&

6条回答
  •  猫巷女王i
    2020-11-28 08:30

    You can have some side-effects in logical expression, for example you can assign simultaneously with checking. This may work wrongly if only one part evaluated.

    Can't remember good example now, but remember that I was in need of "non-short-circuit" operators sometimes.

    Hmmm.... Below is WRONG example, which won't work without "non-short-circuit" OR:

    if( (object1=getInstance1()).getNumber() == 1 || (object2=getInstance2()).getNumber() == 2 ) {
    
        // do something which requires bot object1 and object2 assigned
    
    }
    

提交回复
热议问题