In a project, I see the following code:
//f is a File
boolean acceptable = true;
acceptable &= sweepFilename != null;
acceptable &= f.getName().equals(sweepFilename.toString()); // parsable
acceptable &= id == null || id.equals(sweepFilename.getId());
acceptable &= length == null || length.equals(sweepFilename.getLength());
acceptable &= f.getName().toLowerCase().endsWith(SweepFilename.EXTENSION);
acceptable |= f.isDirectory();
return acceptable;
Can someone explain me what the &= and |= means?
I understand it as, if acceptable is true then also check the right side and assign the value of the operation (false/true) to acceptable, so that if it is false, then it will not need to check the right side.
Just like
a += b;
means
a = a+b;
, you have
a &= b;
meaning
a = a&b;
And of course the same for |=.
You have the same construct for other operators in most languages whose syntax inherits from the C language. Look for example at this : What does ">>=" mean in Linux kernel source code?
See also the complete list of assignment operators in java.
+=, -=, *=, /=, &=, |= and other such operators are in most languages simply shorthands for a = a + b and equivalent.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With