Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strike>I think that a bit-wise OR:</p> <pre><code>if ((a | b) == 0) . . . </code></pre> <p>would work if you want to check specifically for 0. I'm not sure if this saves any execution time. More to the point, it makes for cryptic code, which will make the future maintainer of this code curse you (even if its yourself). I recommend sticking with the more-typing option.</strike></p> <p>Bah. I misread OP's original logic.</p> <p><strong>Another go...</strong></p> <p>If you want to test whether any one of many variables is equal to an expected value, a generic function might work:</p> <pre><code>public &lt;T&gt; boolean exists(T target, T... values) { for (T value : values) { if (target == null) { if (value == null) { return true; } } else if (target.equals(value)) { return true; } } return false; } </code></pre> <p>This will work for any number of objects of one type. Primitives will be autoboxed so it will work with them as well. Your original code will be something like:</p> <pre><code>if (test(0, a, b)) { // do something } </code></pre> <p>(A better method name would be desperately needed to even consider whether this an improvement over what you have now. Even if the test expands to 3 or 4 variables, I question the need for this kind of thing.) Note that this also works with arrays:</p> <pre><code>int[] values = { . . . }; if (test(0, values)) { . . . </code></pre> <p>and it can be used to test whether an array (or any of a collection of variables) is <code>null</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload