-1

I was looking at the android source code today and I found this:

if (mLeftDragger.continueSettling(true) | mRightDragger.continueSettling(true)) {
            ViewCompat.postInvalidateOnAnimation(this);
}

The return type of both these methods is boolean. What is the purpose of this operator and what happens there actually?

Kaloyan Roussev
  • 14,515
  • 21
  • 98
  • 180
  • I'm not sure that is really a duplicate, because the linked question answers the question of what `|` does for integer operands, but not for boolean operands. – ajb Sep 05 '14 at 15:14
  • It is bitwise OR for integer arguments, or non-short-circuit OR for boolean arguments. – Pshemo Sep 05 '14 at 15:14
  • 1
    Described [here][1]. The difference between | and || is that the former is not short-circuit... so even if the first operand is true, the rest will still be evaluated. I believe this is not described in the linked question...? [1]: http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.22.2 – sfThomas Sep 05 '14 at 15:14
  • 1
    It means "or", similar to `||`, but *both* operands will be evaluated. This means that `continueSettling` is called on both objects, no matter whether it returns `true` or `false` for `mLeftDragger`. The result is `true` if either method returns `true`. – ajb Sep 05 '14 at 15:15
  • 1
    I feel like [this question](http://stackoverflow.com/q/7101992/1079354) is a suitable duplicate to this. It deals directly with booleans. – Makoto Sep 05 '14 at 15:18
  • @Makoto Use your hammer! – Sotirios Delimanolis Sep 05 '14 at 15:19
  • How was I supposed to search for this question by the single-character string of "|". Give me a break :) – Kaloyan Roussev Sep 05 '14 at 15:19
  • @SotiriosDelimanolis: I wanted to wait for a relatively simple quorum on the matter. – Makoto Sep 05 '14 at 15:20
  • You know, you could actually look it up in a Java reference. – Hot Licks Sep 05 '14 at 15:28
  • @ajb - A bool is simply a 1-bit integer. – Hot Licks Sep 05 '14 at 15:30
  • 1
    @Hot Licks--Correct, in C++. Java does not have a `bool` type, and its `boolean` type is not simply an integer (and cannot be converted to or from one with a cast), although there is an obvious relation between booleans and the subset {0,1} of integers. So because of that, I don't really consider the _originally_ linked question to be a duplicate since it doesn't deal with `true` and `false` at all. (The duplicate question has changed since my first comment.) – ajb Sep 05 '14 at 15:33
  • @ajb - The reason that boolean is treated a little differently in Java is so that it's easy to require only a boolean value in an `if` statement or for the "logical" operators. This, among other things, allows the compiler to diagnose `if (a = b)` as an error. – Hot Licks Sep 05 '14 at 18:20

1 Answers1

0

|| short circuits where as | does not. Short circuiting is when the right hand expression does not get evaluated if there is no need for it to be evaluated. | will calculate all Boolean expressions (left to right). Check out this answer.

Community
  • 1
  • 1