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

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

    ‘&&’ : - is a Logical AND operator produce a boolean value of true or false based on the logical relationship of its arguments.

    For example: - Condition1 && Condition2

    If Condition1 is false, then (Condition1 && Condition2) will always be false, that is the reason why this logical operator is also known as Short Circuit Operator because it does not evaluate another condition. If Condition1 is false , then there is no need to evaluate Condtiton2.

    If Condition1 is true, then Condition2 is evaluated, if it is true then overall result will be true else it will be false.

    ‘&’ : - is a Bitwise AND Operator. It produces a one (1) in the output if both the input bits are one. Otherwise it produces zero (0).

    For example:-

    int a=12; // binary representation of 12 is 1100

    int b=6; // binary representation of 6 is 0110

    int c=(a & b); // binary representation of (12 & 6) is 0100

    The value of c is 4.

    for reference , refer this http://techno-terminal.blogspot.in/2015/11/difference-between-operator-and-operator.html

提交回复
热议问题