Java boolean |= operator
Asked Answered
C

5

30

Recently I saw a code using this:

boolean val = something();
val |= somethingElse();

Interesting part is |= (binary like) operator made on boolean primitive type.

It surprised me that |= exist for boolean, as if it was integer type, and searched Java specification for this operator, but could not find any.

I'd be curious if right operand is evaluated if left value already is true.

Can someone point me to Java specification of this?

Chantel answered 14/3, 2013 at 13:52 Comment(6)
It is merely val = val | somethingElse(); // somethingElse() expected to return boolean. It is a standard ORNecessitate
@SudhanshuUmalkar OP knows that, he asks for specification in a document.Galsworthy
answer is here dudes: https://mcmap.net/q/217809/-shortcut-quot-or-assignment-quot-operator-in-java/544983Galsworthy
I thought there is only || for boolean expressions, so I would expect operator like ||= . Does it make sense to make binary | on boolean?Chantel
No shortcut evaluation, though not mentioned explicitly. Only at || the shortcut logic is mentioned explicitly.Egypt
@Sudhanshu it's not a standard OR, it's a bitwise OR operator (as opposed to a boolean operator). It is also an aggressive operator, as opposed a short-circuit operator (such as the boolean operator).Francenefrances
B
38

From the JLS:

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.

15.22.2. Boolean Logical Operators &, ^, and |

When both operands of a &, ^, or | operator are of type boolean or Boolean, then the type of the bitwise operator expression is boolean. In all cases, the operands are subject to unboxing conversion (§5.1.8) as necessary.

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

This means that

val |= somethingElse();

is strictly equivalent to

val = val | somethingElse();

(assuming somethingElse() returns boolean or Boolean).

I'd be curious if right operand is evaluated if left value already is true.

Yes, it would be evaluated, since | does not short-circuit:

15.7.2. Evaluate Operands before Operation

The Java programming language guarantees that every operand of an operator (except the conditional operators &&, ||, and ? :) appears to be fully evaluated before any part of the operation itself is performed.

15.24. Conditional-Or Operator ||

Thus, || computes the same result as | on boolean or Boolean operands. It differs only in that the right-hand operand expression is evaluated conditionally rather than always.

Bigmouth answered 14/3, 2013 at 13:55 Comment(1)
val |= somethingElse(); has no equivalent in Kotlin. I wonder why...Eldoria
V
6

See http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.22.2 for the definition of |. See http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.26.2 for the defintion of |=. The definitions are just what you would think.

What surprises me is the lack of a ||= operator.

Voyageur answered 14/3, 2013 at 13:57 Comment(0)
B
3

The bitwise logic operators will have the same effect of "normal" logic operators on booleans.

From Java Language Specification 15.22:

When both operands of a &, ^, or | operator are of type boolean or Boolean, then the type of the bitwise operator expression is boolean. In all cases, the operands are subject to unboxing conversion (§5.1.8) as necessary.

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

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

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

The only real difference is that bitwise operators cant be used to short-circuit evaluation.

For example, this code will throw NullPointerException:

Boolean b1 = new Boolean(true);
Boolean b2 = null;
if (b1 || b2) {
    //no null pointer here;
}
if (b1 | b2) {
    //null pointer here;
}
Bialy answered 14/3, 2013 at 13:54 Comment(0)
S
2
>>I'd be curious if right operand is evaluated if left value already is true.

Bitwise operators (like |, &, ..) evaluate both sides, before completion.

Logical operators (like &&, ||, ..) can skip the evaluation of the second part in some cases. This is called short-circuit.

Shirk answered 14/3, 2013 at 14:1 Comment(0)
A
-1

It is not binary it's an "OR" logic statement means

 val |= {something else} 

is same as the Boolean expression:

val == val or {something else} 

which is inclusive or ( the regular or used in mathematics expressions and computer science)

In some programming languages or is noted by two || signs and in some by one | one of them is SQL and all database languages that I know of DTD JSON etc..

Agenda answered 14/3, 2013 at 16:47 Comment(1)
| is not the conditional OR (||), it's the bitwise OR. So val |= something is the same as val = val | something.Telephone

© 2022 - 2024 — McMap. All rights reserved.