-1

I'm just curious how java works. Can someone explain why getBoolean is called in Case 1 and not called in Case 2?

public class Main {

    public static void main(String[] args) {
        System.out.println("---------- Case 1 ----------");
        boolean b = false;
        b &= getBoolean(true);

        System.out.println("---------- Case 2 ----------");
        b = false;
        b = b && getBoolean(true);
    }

    private static boolean getBoolean(boolean bool) {
        System.out.println("getBoolean(" + bool + ") was called\n");
        return bool;
    }

}

Output:

---------- Case 1 ----------
getBoolean(true) was called

---------- Case 2 ----------
Egis
  • 5,081
  • 5
  • 39
  • 61
  • 3
    logical versus bitwise AND – Sneftel Jul 14 '14 at 10:25
  • It works like the `&` operator does. Every compound operator works like the equivalent operator without the `=` - so `+=` is like `+`, `-=` is like `-`, etc. Why would you expect `&=` to be like `&&`? – Jon Skeet Jul 14 '14 at 10:27
  • @Sneftel - there is no difference between "logical" and "bitwise" for booleans, because they are just one active bit. What matters here is that `&&` is short-circuit, but `&` and `&=` are not. – Dawood ibn Kareem Jul 14 '14 at 10:27
  • That was pretty much my point, yes. – Sneftel Jul 14 '14 at 10:29
  • 1
    Really? Because it's not what you said at all. – Dawood ibn Kareem Jul 14 '14 at 10:30
  • possible duplicate of [Shortcut "or-assignment" (|=) operator in Java](http://stackoverflow.com/questions/2486472/shortcut-or-assignment-operator-in-java) – Not a bug Jul 14 '14 at 10:30
  • & does not short circuit *because* it is treated as a bitwise op. This behavior was copied directly from C; Java dropped some of the integer-ness of booleans (and most of the boolean-ness of integers), but kept the ability to perform this integer op on booleans because of the short-circuiting difference. – Sneftel Jul 14 '14 at 10:50

5 Answers5

5

b &= a is a shortcut to b = b & a not to b = b && a

It is because of the difference between & and && operators.

  • The & operator always evaluates both sides of conditions.

  • The && operator evaluates the second only if needed.

So, getBoolean(true) will not run in the 2nd case.

Stelium
  • 1,207
  • 1
  • 12
  • 23
4

In Case 2 getBoolean(true); is not evaluated, because the && is a short-circuit operator, e.g. if the expression will be false it stops evaluating

Case 1 just sets the result of the getBoolean() method to the b variable.


Update, thanks to @KisHan:

Note that b &= a is equal to b = b & a and not b = b && a

Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
3

In second case b is false and for && if first condition is false it won't move ahead.

 b = b && getBoolean(true);//b is false

So &&(Logical AND) first checks the left side of the operation, if it's true than it continues to the right side.

While &(Bitwise AND) operator evaluates both sides of expression as it's bitwise AND operator.As it performs AND operation between leftside and right side.

So in first case it will be

b = b & getBoolean(true);//will perform AND operation to evaluate Expression
akash
  • 22,664
  • 11
  • 59
  • 87
2

in first case it's bitwise and trying to get the assigned value (getBoolean()).

in second case the && does a short circuit evaluation. Where if first expression is false then the second won't evaluate.

in which the second argument is only executed or evaluated if the first argument does not suffice to determine the value of the expression:

b = b && getBoolean(true); // b already false and there is no use even getBoolean(true) is true or false , because the result is already false as b is false.
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

& means bitwise AND: on a bit-by-bit level, it sets the output to true if both of the input bits were true. && means logical AND: if both inputs are truthy, the expression returns true. && is also short-circuiting (stops evaluation and returns false immediately if the first input is falsy), which is what you're seeing here.

Patrick Collins
  • 10,306
  • 5
  • 30
  • 69
  • They're booleans - it doesn't make sense to distinguish between "bitwise" and "logical" for booleans, because there is only one meaningful bit. – Dawood ibn Kareem Jul 14 '14 at 10:29
  • @DavidWallace it doesn't make sense to talk about a "short-circuiting bitwise operation." The OP obviously does not understand the difference between a bitwise and logical AND, any answer that doesn't explain what's going on here is incomplete. – Patrick Collins Jul 14 '14 at 10:31
  • If you understand bitwise vs logical, the fact that `&` does not short circuit follows immediately as a consequence. If you don't explain what a bitwise AND is, then it doesn't make any sense. It's as if the OP asked "I don't understand why it's easier to drive nails with a hammer than a screwdriver," and you didn't explain what a screw is. – Patrick Collins Jul 14 '14 at 10:32
  • There IS no difference between bitwise and logical if there's only one bit. `&&` is bitwise AND logical AND short-circuiting. `&` is bitwise AND logical AND not short-circuiting. Talking about "bitwise" and "logical" for booleans is a complete red herring. – Dawood ibn Kareem Jul 14 '14 at 10:33
  • `&` and `&&` are completely different operations, the fact that booleans are only a single bit is not the point. This is a special case of `any_truthy_value & function_call()`, and a complete answer explains the general case. – Patrick Collins Jul 14 '14 at 10:34