This question already has answers here:
Why do we usually use || over |? What is the difference?
(28 answers)
Closed 8 years ago.
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?
|| 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.
Related
This question already has answers here:
Compare one String with multiple values in one expression
(20 answers)
String.equals() with multiple conditions (and one action on result) [duplicate]
(7 answers)
Closed 4 years ago.
Need help with if else statement in Java. Need the program to output when either l.getPlot().equals("MR") or ("X") and if l.getZone().equals("UP SPEC") set the top upper limit.
Can anyone explain to me how to properly set that up so when the query is a match for MR or X it will set the top upper.
Note: If I remove || ("X") it works for all the MR items but leaves all the ("X") blank.
if (l.getPlot().equals("MR")) || ("X"){
if (l.getZone().equals("UP SPEC")) {
limit.setTopUpper(l.getLimit());
} else if (l.getZone().equals("LO SPEC")) {
limit.setTopLower(l.getLimit());
}
}
This should do the trick because it applies the logical "or" to two conditions, instead of one condition and one bare string.
if (l.getPlot().equals("MR")) || (l.getPlot().equals("X")){
Here is an improved version (credits Tim Biegeleisen), which avoids a null pointer exception.
if ("MR".equals(l.getPlot()) || "X".equals(l.getPlot()))
If the argument to equals() evaluates to NULL, the result is a clean false.
This question already has answers here:
&& (AND) and || (OR) in IF statements
(9 answers)
Closed 6 years ago.
I have a doubt of how this if loop is going to work. if fn1 evaluates to be true, will it still go for checking fn2 or will it go into the if loop and add the elements into the list?
The || operator is short-circuited, meaning that if the left-hand operand is true (whatever that means in the language in question), the right-hand operator isn't evaluated at all. So in your example, fn1 will definitely be called, but fn2 will only be called if fn1 returns false.
This question already has answers here:
Differences in boolean operators: & vs && and | vs ||
(11 answers)
Closed 7 years ago.
In Java, the & operator evaluates both the operands and so does the || operator.
I do understand the difference between && (AND) and || (OR) operators. I want to gain some understanding on bitwise & operators and the logical || operator.
What is the difference between them? Which one to use when?
The difference is in something called "short circuiting".
The & and | operators always evaluate both sides of the operands.The && and || operators "short circuits" the operation by not evaluating the right operands if it isn't necessary.
This question already has answers here:
Does Python have a ternary conditional operator?
(31 answers)
Closed 9 years ago.
In Java, setting a variable to a value based on a condition could be done in one line
like so:
variable = (!true) ? 1 : 2
This would result to '2'.
Is there python equivalent to this code?
Thank you.
variable = 1 if not True else 2
General ternary syntax:
<value_if_true> if <condition> else <value_if_false>
This is called a conditional expression in Python, and is mostly equivalent to the "ternary operator" in C-family languages (although it's not actually an operator). The original proposal PEP 308 has more details.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Java Operators : |= bitwise OR and assign example
boolean bAlive;
bAlive |= this.properties.containsKey(name);
In the above , the code uses '|'.
Why using '|' ?
Thanks in advance.
The boolean is being OR'ed with the value on the right.
If this.properties.containsKey(name) is TRUE, then bAlive is set to TRUE.
Otherwise, bAlive remains the same.