What is the difference between & and && in Java?

前端 未结 13 2455
感情败类
感情败类 2020-11-22 10:33

I always thought that && operator in Java is used for verifying whether both its boolean operands are true, and the & oper

13条回答
  •  感情败类
    2020-11-22 11:15

    boolean a, b;
    
    Operation     Meaning                       Note
    ---------     -------                       ----
       a && b     logical AND                    short-circuiting
       a || b     logical OR                     short-circuiting
       a &  b     boolean logical AND            not short-circuiting
       a |  b     boolean logical OR             not short-circuiting
       a ^  b     boolean logical exclusive OR
      !a          logical NOT
    
    short-circuiting        (x != 0) && (1/x > 1)   SAFE
    not short-circuiting    (x != 0) &  (1/x > 1)   NOT SAFE
    

提交回复
热议问题