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

前端 未结 13 2651
感情败类
感情败类 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 10:53

    It depends on the type of the arguments...

    For integer arguments, the single ampersand ("&")is the "bit-wise AND" operator. The double ampersand ("&&") is not defined for anything but two boolean arguments.

    For boolean arguments, the single ampersand constitutes the (unconditional) "logical AND" operator while the double ampersand ("&&") is the "conditional logical AND" operator. That is to say that the single ampersand always evaluates both arguments whereas the double ampersand will only evaluate the second argument if the first argument is true.

    For all other argument types and combinations, a compile-time error should occur.

提交回复
热议问题