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

前端 未结 13 2464
感情败类
感情败类 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.

    0 讨论(0)
  • 2020-11-22 10:56

    && and || are called short circuit operators. When they are used, for || - if the first operand evaluates to true, then the rest of the operands are not evaluated. For && - if the first operand evaluates to false, the rest of them don't get evaluated at all.

    so if (a || (++x > 0)) in this example the variable x won't get incremented if a was true.

    0 讨论(0)
  • & <-- verifies both operands
    && <-- stops evaluating if the first operand evaluates to false since the result will be false

    (x != 0) & (1/x > 1) <-- this means evaluate (x != 0) then evaluate (1/x > 1) then do the &. the problem is that for x=0 this will throw an exception.

    (x != 0) && (1/x > 1) <-- this means evaluate (x != 0) and only if this is true then evaluate (1/x > 1) so if you have x=0 then this is perfectly safe and won't throw any exception if (x != 0) evaluates to false the whole thing directly evaluates to false without evaluating the (1/x > 1).

    EDIT:

    exprA | exprB <-- this means evaluate exprA then evaluate exprB then do the |.

    exprA || exprB <-- this means evaluate exprA and only if this is false then evaluate exprB and do the ||.

    0 讨论(0)
  • 2020-11-22 11:04

    Besides not being a lazy evaluator by evaluating both operands, I think the main characteristics of bitwise operators compare each bytes of operands like in the following example:

    int a = 4;
    int b = 7;
    System.out.println(a & b); // prints 4
    //meaning in an 32 bit system
    // 00000000 00000000 00000000 00000100
    // 00000000 00000000 00000000 00000111
    // ===================================
    // 00000000 00000000 00000000 00000100
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-11-22 11:05

    With booleans, there is no output difference between the two. You can swap && and & or || and | and it will never change the result of your expression.

    The difference lies behind the scene where the information is being processed. When you right an expression "(a != 0) & ( b != 0)" for a= 0 and b = 1, The following happens:

    left side: a != 0 --> false
    right side: b 1= 0 --> true
    left side and right side are both true? --> false
    expression returns false
    

    When you write an expression (a != 0) && ( b != 0) when a= 0 and b = 1, the following happens:

    a != 0 -->false
    expression returns false
    

    Less steps, less processing, better coding, especially when doing many boolean expression or complicated arguments.

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