Difference between Java's || and & operator [duplicate] - java

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.

Related

Why is the output "false10" and not "false11"? [duplicate]

This question already has answers here:
Why isn't the right side of && operator evaluated? [duplicate]
(4 answers)
Closed 3 years ago.
Could someone tell me why the value of a is not changing in the following code?
int a = 10;
System.out.print( (a < 5) && (++a==11));
System.out.println(a);
You're seeing "short circuit evaluation" in action. The second part of the boolean expression (++a==11) is never evaluated, because (a < 5) is false. In this case, the JVM knows that the whole expression is false before it evaluates (++a==11), so it skips it entirely.
This is also a great example of why such "side effects" are bad in logical tests: you're mutating the values you're evaluating in an unpredictable way. In a non-trivial program, you don't necessarily know whether (a < 5) is true, so it's difficult to know whether a will be incremented or not.

Why is the following statement legal in C but not in Java? [duplicate]

This question already has an answer here:
Ternary operator in java vs c [duplicate]
(1 answer)
Closed 4 years ago.
Why is the following statement legal in C but not in Java?
int k = 1;
(10 < 20) ? k++ : k--;
This is because in C all expressions can be made into expression-statements by adding a semicolon ;.
In Java not all expressions can be made into expression statements. They must be assignment expressions, use postfix/prefix operators, be method invocations, or new expressions. See more here
Further, a ternary operator in Java requires that each operand be a non void expression and the value returned must be assigned.

Java Why is char = char^char different from char^= char? [duplicate]

This question already has answers here:
Why don't Java's +=, -=, *=, /= compound assignment operators require casting?
(11 answers)
Closed 6 years ago.
I was solving a problem in leet code and noticed the following code was not allowed in Java,
char c = 's';
c = c^c;
While the following was
char c = 's';
c^=c;
Is there a particular reason? Thanks you.
This is also true for plus or minus. c^c is evaluated as an int so the right hand side is int and can not be assigned into a char.
In the ^= case, the right hand side is char, and can be applied onto a char.
This is not the most obvious behavior.

if( fn1(args) || fn2(args){ "Add the elements into the list" ;} [duplicate]

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.

Java single pipeline operator (expression | expression) [duplicate]

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.

Categories

Resources