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

前端 未结 13 2452
感情败类
感情败类 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:17

    it's as specified in the JLS (15.22.2):

    When both operands of a &, ^, or | operator are of type boolean or Boolean, then the type of the bitwise operator expression is boolean. In all cases, the operands are subject to unboxing conversion (§5.1.8) as necessary.

    For &, the result value is true if both operand values are true; otherwise, the result is false.

    For ^, the result value is true if the operand values are different; otherwise, the result is false.

    For |, the result value is false if both operand values are false; otherwise, the result is true.

    The "trick" is that & is an Integer Bitwise Operator as well as an Boolean Logical Operator. So why not, seeing this as an example for operator overloading is reasonable.

    0 讨论(0)
提交回复
热议问题