Why doesn't Java have compound assignment versions of the conditional-and and conditional-or operators? (&&=, ||=)

polygenelubricants picture polygenelubricants · Feb 24, 2010 · Viewed 15.6k times · Source

So for binary operators on booleans, Java has &, |, ^, && and ||.

Let's summarize what they do briefly here:

For &, the result value is true if both operand values are true; otherwise, the result is false.

For |, the result value is false if both operand values are false; otherwise, the result is true.

For ^, the result value is true if the operand values are different; otherwise, the result is false.

The && operator is like & but evaluates its right-hand operand only if the value of its left-hand operand is true.

The || operator is like |, but evaluates its right-hand operand only if the value of its left-hand operand is false.

Now, among all 5, 3 of those have compound assignment versions, namely |=, &= and ^=. So my question is obvious: why doesn't Java provide &&= and ||= as well? I find that I need those more than I need &= and |=.

And I don't think that "because it's too long" is a good answer, because Java has >>>=. There must be a better reason for this omission.


From 15.26 Assignment Operators:

There are 12 assignment operators; [...] = *= /= %= += -= <<= >>= >>>= &= ^= |=


A comment was made that if &&= and ||= were implemented, then it would be the only operators that do not evaluate the right hand side first. I believe this notion that a compound assignment operator evaluates the right hand side first is a mistake.

From 15.26.2 Compound Assignment Operators:

A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T)((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.

As proof, the following snippet throws a NullPointerException, not an ArrayIndexOutOfBoundsException.

    int[] a = null;
    int[] b = {};
    a[0] += b[-1];

Answer

oHo picture oHo · Mar 29, 2012

Reason

The operators &&= and ||= are not available on Java because for most of the developers these operators are:

  • error-prone
  • useless

Example for &&=

If Java allowed &&= operator, then that code:

bool isOk = true; //becomes false when at least a function returns false
isOK &&= f1();
isOK &&= f2(); //we may expect f2() is called whatever the f1() returned value

would be equivalent to:

bool isOk = true;
if (isOK) isOk = f1();
if (isOK) isOk = f2(); //f2() is called only when f1() returns true

This first code is error-prone because many developers would think f2() is always called whatever the f1() returned value. It is like bool isOk = f1() && f2(); where f2() is called only when f1() returns true.

If the developer wants f2() to be called only when f1() returns true, therefore the second code above is less error-prone.

Else &= is sufficient because the developer wants f2() to be always called:

Same example but for &=

bool isOk = true;
isOK &= f1();
isOK &= f2(); //f2() always called whatever the f1() returned value

Moreover, the JVM should run this above code as the following one:

bool isOk = true;
if (!f1())  isOk = false;
if (!f2())  isOk = false;  //f2() always called

Compare && and & results

Are the results of operators && and & the same when applied on boolean values?

Let's check using the following Java code:

public class qalcdo {

    public static void main (String[] args) {
        test (true,  true);
        test (true,  false);
        test (false, false);
        test (false, true);
    }

    private static void test (boolean a, boolean b) {
        System.out.println (counter++ +  ") a=" + a + " and b=" + b);
        System.out.println ("a && b = " + (a && b));
        System.out.println ("a & b = "  + (a & b));
        System.out.println ("======================");
    }

    private static int counter = 1;
}

Output:

1) a=true and b=true
a && b = true
a & b = true
======================
2) a=true and b=false
a && b = false
a & b = false
======================
3) a=false and b=false
a && b = false
a & b = false
======================
4) a=false and b=true
a && b = false
a & b = false
======================

Therefore YES we can replace && by & for boolean values ;-)

So better use &= instead of &&=.

Same for ||=

Same reasons as for &&=:
operator |= is less error-prone than ||=.

If a developer wants f2() not to be called when f1() returns true, then I advice the following alternatives:

// here a comment is required to explain that 
// f2() is not called when f1() returns false, and so on...
bool isOk = f1() || f2() || f3() || f4();

or:

// here the following comments are not required 
// (the code is enough understandable)
bool isOk = false;
if (!isOK) isOk = f1();
if (!isOK) isOk = f2(); //f2() is not called when f1() returns false
if (!isOK) isOk = f3(); //f3() is not called when f1() or f2() return false
if (!isOK) isOk = f4(); //f4() is not called when ...