Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the ampersand-equals operator used for in Java?

Does Java have an &= operator? I'm seeing this code:

boolean allReady = true;            
for(Entry<String,Boolean> ace : factory.readyAces.entrySet()) {
    allReady &= ace.getValue();

What is &=?

like image 243
Caffeinated Avatar asked May 28 '12 18:05

Caffeinated


People also ask

What does ampersand do in Java?

The & operator in Java has two definite functions: As a Relational Operator: & is used as a relational operator to check a conditional statement just like && operator. Both even give the same result, i.e. true if all conditions are true, false if any one condition is false.

Can you use ampersand in Java?

Java has two operators for performing logical And operations: & and &&. Both combine two Boolean expressions and return true only if both expressions are true.

What is & vs && Java?

& is a bitwise operator and compares each operand bitwise. It is a binary AND Operator and copies a bit to the result if it exists in both operands. (A & B) will give 12 which is 0000 1100. Whereas && is a logical AND operator and operates on boolean operands.

Why do we use && in Java?

The symbol && denotes the AND operator. It evaluates two statements/conditions and returns true only when both statements/conditions are true.


Video Answer


1 Answers

It's the same as:

allReady = allReady & ace.getValue();

This is a bit-wise and. It means you always evaluate both sides, then take the "logical and" (result is only true if both sides are true).

like image 174
Matthew Flaschen Avatar answered Sep 28 '22 17:09

Matthew Flaschen