Why does the "or" go before the "and"? - java

int it=9, at=9;
if(it>4 || ++at>10 && it>0)
{
System.out.print("stuff");
}
System.out.print(at);
prints out stuff9 and I want to know why as I thought ++at>10 && it>0 would be evaluated first and thus make at = 10.

Operator precedence only controls argument grouping; it has no effect on execution order. In almost all cases, the rules of Java say that statements are executed from left to right. The precedence of || and && causes the if control expression to be evaluated as
it>4 || (++at>10 && it>0)
but the higher precedence of && does not mean that the && gets evaluated first. Instead,
it>4
is evaluated, and since it's true, the short-circuit behavior of || means the right-hand side isn't evaluated at all.

Your compound expression is equivalent to
if(it>4 || (++at>10 && it>0))
due to Java operator precedence rules. The operands of || are evaluated left to right, so it>4 is evaluated first, which is true, so the rest of it (the entire && subexpression) doesn't need to be evaluated at all (and so at does not change).

I feel I should add how to actually get your expected output.
Brief explanation: the || operator short-circuits the entire expression, thus the right side not even being evaluated.
But, to not short-circuit and get your expected output, instead of the || operator, just use |, like so:
int a = 9, b = 9;
if(a>4 | ++b>10 && a>0)
{
System.out.print("stuff");
}
System.out.print(b);
The output to that is stuff10
This way, regardless of one side being true or not, they're both still evaluated.
The same thing goes for && and &

Related

Short circuiting logical operators, precedence, and evaluation order? [duplicate]

In JavaScript and Java, the equals operator (== or ===) has a higher precedence than the OR operator (||). Yet both languages (JS, Java) support short-circuiting in if statements:
When we have if(true || anything()), anything() isn't evaluated.
You can also have the following expression: true || foo == getValue()) - for example in an output statement such as console.log(...);, or in an assignment.
Now, according to operator precedence, short-circuiting shouldn't happen, as === = == > || in terms of precedence. (In other words, the comparison should happen first, for which getValue() ought to be called, as the equality check has a higher precedence that the OR comparison.) But it does. getValue() isn't called (as can easily be checked by putting an output statement into its body).
Why (does short circuiting work when the operator precedence says it shouldn't)?
Or am I confusing matters?
Or am I confusing matters?
You are. I think it's much simpler to think about precedence as grouping than ordering. It affects the order of evaluation, but only because it changes the grouping.
I don't know about Javascript for sure, but in Java operands are always evaluated in left-to-right order. The fact that == has higher precedence than || just means that
true || foo == getValue()
is evaluated as
true || (foo == getValue())
rather than
(true || foo) == getValue()
If you just think about precedence in that way, and then consider that evaluation is always left-to-right (so the left operand of || is always evaluated before the right operand, for example) then everything's simple - and getValue() is never evaluated due to short-circuiting.
To remove short-circuiting from the equation, consider this example instead:
A + B * C
... where A, B and C could just be variables, or they could be other expressions such as method calls. In Java, this is guaranteed to be evaluated as:
Evaluate A (and remember it for later)
Evaluate B
Evaluate C
Multiply the results of evaluating B and C
Add the result of evaluating A with the result of the multiplication
Note how even though * has higher precedence than +, A is still evaluated before either B or C. If you want to think of precedence in terms of ordering, note how the multiplication still happens before the addition - but it still fulfills the left-to-right evaluation order too.
According to the language specification, https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.24
At run time, the left-hand operand expression is evaluated first; if the result has type Boolean, it is subjected to unboxing conversion (§5.1.8).
If the resulting value is true, the value of the conditional-or expression is true and the right-hand operand expression is not evaluated.
So if you have a || b==c, it is not interpreted as (a || b) == c, because || has lower precedence, as you found in the tutorial. Instead, it is interpreted as a || (b==c). Now since a is the left side of ||, it is evaluated first.
There is no operator precedence in this case. What you are questioning is like in f(callback) statement the callback function being evaluated even before f. This can not happen.
On the other hand, in JS the || is one of the few places where you can watch laziness at show. The == operand (think as if it is an infix function like in fully functional languages) takes two arguments and the one on the left gets evaluated first. If it resolves to true the second argument doesn't even get evaluated.

Working of short circuit and unary operator

Please have a look at the following code:
int i=5;
boolean b = i<5 && ++i<5;//line 2
System.out.println(i);//line 3, prints 5
In line 2, according to my understanding: Since among all the operators, ++ has highest precedence ++i should be evaluated first. But line 3 actually is printing i=5 (and not 6). Meaning, && has evaluated before ++ operator. How is it possible?
EDIT: From the answers I see that "In Java, all expressions are evaluated from left to right.". But when does actually precedence order comes into play. In following code:
int a=1,b=1,c=1;
boolean b = a==b&&b==c;//Line2
In line2 code would't just run from left to right. First a==b is evaluated then b==c and then && operator. Can you please explain more?
That's not how the expression is processed.
In Java, all expressions are evaluated from left to right. Operator precedence only comes into play when considering the evaluation of the arguments of &&.
So i < 5 is computed before ++i < 5 is even considered.
In this case ++i < 5 will not be evaluated, since i < 5 is false. So i stays at 5.
The key is: ++ has the highest precedence inside a expression, but the sentence boolean b = i<5 && ++i<5 has two expressions evaluated from left to right.
Think of b = i<5 && ++i<5 like:
if( i<5 ){
if ( ++i<5 ){
return true;
}
}
Considering your edited answer, the second code snippet also evaluates from left to right.
In both cases there is an expression of the form A && B with A as the left argument and B as the right argument of &&.
Here is where "left-to-right" applies. First, the left expression A is evaluated. In your first code snippet, that is i<5 and since this expression evaluates to false, the second argument to that isn't even considered, since the whole expression will evaluate to false (short-circuiting).
In your second snippet you have as left argument a==b. Since that evaluates to true, only now the second argument is considered, which also evaluates to true. That leaves you with true && true which can then be processed further.
Your misunderstanding is in how the '&&' works. When you use them java uses short circuit or lazy evaluation.
boolean equal = 1==1 && 1==2 java evaluates the 1==1 sees it as true, checks the ampersands and has to evaluate the 1==2 which is false and therefore equal is false.
boolean equal = 1==2 && 1==1 java evaluates the 1==2 sees it as false, then checks the ampersands and can see that whether the next expression is true or false the outcome is the same, that equal is false. Therefor it never evaluates the second equation.
More plainly the second example is saying boolean equal = false & true. This can only be true if both are true. Thus knowing the first is false is all we need to know the outcome. By ignoring the sencond expression at this point we can save needless calculations.

The precedence of java boolean

&& is supposed to have higher precedence than ||:
int a = 10;
System.out.println(a==10 || --a==9 && a==8);
System.out.println(a);
this prints true, and 10. It seems that it checks only the first part and if its true the second never executes.
If I do a==9 then the second part seems to evaluate and prints 9. Can someone explain why this is happening? Isn't && supposed to evaluate first?
Edit: I see it. a==10 || --a==9 && a==8 transforms into (a==10) || (a==9 && a==8) So if the first one evaluates to true it short circuits. Thank you for your help guys.
System.out.println(a==10 || --a==9 && a==8);
equal to
System.out.println((a==10) || (((--a)==9) && (a==8)));
Java evaluates || first. Find out that left part (a==10) is true and derive that no mater what is in right part result anyway will be true
PS: it is common trick to write
if(obj != null && obj.isSomething) { ... }
to prevent NPE
It's useful to look at expressions as trees.
a==10 || --a==9 && a==8
In this example, the top of the tree is an OR. The left branch is an EQUALS, and the right branch is an AND whose left and right branch are both EQUALS.
Since I'm awful at graphics, it would look something like:
OR
/ \-----
= \
/ \ AND
a 10 / \
= =
/ \ / \
--a 9 a 8
The "precedence" that you're talking about, orders this tree. But it doesn't necessarily change execution order. Because in this case, this is true if the first branch is true, so no need to check the second branch.
If you evaluate (a==10) || (b == 9) (which is an OR operation),
if the first clause is true, then the entire expression must be true (because it's OR) and the second clause will not be executed for reasons of optimization.
Code is executed from left to right, so you'll hit the first TRUE therefore your entire statement will be TRUE, and the remainder of your statement will not be executed
If your first clause is false, then only will the second clause be evaluated.
If you use (a==10) | (b == 9) both clauses will be evaluated.
If you want to be certain of the precedence of your operators, you should use brackets
It doesn't have anything to do with higher precedence in this case. Look up lazy evaluation. The operation will be evaluated from left to right and will find that a==10 is true and stop there.
Look at the comments in http://introcs.cs.princeton.edu/java/11precedence/ under order of evaluation
|| and && are short circuit operators they don't evaluate the right hand side expression
if the left hand side is true.
So (--a) is never executed as (a==10) left hand side is true
Therefore the value of a remains same.
**& and | operators that always evaluate both sides.

What is the difference between & and && in if statement in java? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Difference in & and &&
if (true && true) {
System.out.println("test if");
}
if (true & true) {
System.out.println("test if");
}
both are give same output.why ?
&& short-circuits, wheras & doesn't. Example:
if (methodA() && methodB()) {
...
}
In that case, if methodA already returns false, methodB is not called. Formally, if you have an expression a && b, b is only evaluated if a evaluated to true.
Apart from the obvious performance benefit, this is particularly useful for null checks:
if (x != null && x.getSomeValue()) {
...
}
If you used a s single & here, x.getSomeValue() would be evaluated even if x were null, resulting in an exception.
& is a bit operation
&& logically links two booleans
Please see:
http://www.jguru.com/faq/view.jsp?EID=16530
It depends on the type of the arguments...
For integer arguments, the single ampersand ("&")is the "bit-wise AND"
operator. The double ampersand ("&&") is not defined for anything but
two boolean arguments.
For boolean arguments, the single ampersand constitutes the
(unconditional) "logical AND" operator while the double ampersand
("&&") is the "conditional logical AND" operator. That is to say that
the single ampersand always evaluates both arguments whereas the
double ampersand will only evaluate the second argument if the first
argument is true.
For all other argument types and combinations, a compile-time error
should occur.
In the first case, you don't test the second part of the if : the && operator executes from left to right and stops at the first false value.
This may be useful in this case :
if (a!=null && a.doSomething()==23) {
because it prevents a nullPointerException.
When you test boolean conditions, always use &&.
In & statetment it will check both left side and right side statements.in && will check only one side of statement(if one side is true it will not check other side)
&& is the logical AND
so you want to make sure BOTH statements become true.
& is the bitwise AND operator
Have a look here:
http://www.javaforstudents.co.uk/TrueFalse
&& is logical and.
true && true is true and everything else is false.
& is bitwise and.
(10 & 8) = 8

&& (AND) and || (OR) in IF statements

I have the following code:
if(!partialHits.get(req_nr).containsKey(z) || partialHits.get(req_nr).get(z) < tmpmap.get(z)){
partialHits.get(z).put(z, tmpmap.get(z));
}
where partialHits is a HashMap. What will happen if the first statement is true? Will Java still check the second statement? Because in order the first statement to be true, the HashMap should not contain the given key, so if the second statement is checked, I will get NullPointerException.
So in simple words, if we have the following code
if(a && b)
if(a || b)
would Java check b if a is false in the first case and if a is true in the second case?
No, it will not be evaluated. And this is very useful. For example, if you need to test whether a String is not null or empty, you can write:
if (str != null && !str.isEmpty()) {
doSomethingWith(str.charAt(0));
}
or, the other way around
if (str == null || str.isEmpty()) {
complainAboutUnusableString();
} else {
doSomethingWith(str.charAt(0));
}
If we didn't have 'short-circuits' in Java, we'd receive a lot of NullPointerExceptions in the above lines of code.
Java has 5 different boolean compare operators: &, &&, |, ||, ^
& and && are "and" operators, | and || "or" operators, ^ is "xor"
The single ones will check every parameter, regardless of the values, before checking the values of the parameters.
The double ones will first check the left parameter and its value and if true (||) or false (&&) leave the second one untouched.
Sound compilcated? An easy example should make it clear:
Given for all examples:
String aString = null;
AND:
if (aString != null & aString.equals("lala"))
Both parameters are checked before the evaluation is done and a NullPointerException will be thrown for the second parameter.
if (aString != null && aString.equals("lala"))
The first parameter is checked and it returns false, so the second paramter won't be checked, because the result is false anyway.
The same for OR:
if (aString == null | !aString.equals("lala"))
Will raise NullPointerException, too.
if (aString == null || !aString.equals("lala"))
The first parameter is checked and it returns true, so the second paramter won't be checked, because the result is true anyway.
XOR can't be optimized, because it depends on both parameters.
No it will not be checked. This behaviour is called short-circuit evaluation and is a feature in many languages including Java.
All the answers here are great but, just to illustrate where this comes from, for questions like this it's good to go to the source: the Java Language Specification.
Section 15:23, Conditional-And operator (&&), says:
The && operator is like & (§15.22.2), but evaluates its right-hand operand only if the value of its left-hand operand is true. [...] At run time, the left-hand operand expression is evaluated first [...] if the resulting value is false, the value of the conditional-and expression is false and the right-hand operand expression is not evaluated. If the value of the left-hand operand is true, then the right-hand expression is evaluated [...] the resulting value becomes the value of the conditional-and expression. Thus, && computes the same result as & on boolean operands. It differs only in that the right-hand operand expression is evaluated conditionally rather than always.
And similarly, Section 15:24, Conditional-Or operator (||), says:
The || operator is like | (§15.22.2), but evaluates its right-hand operand only if the value of its left-hand operand is false. [...] At run time, the left-hand operand expression is evaluated first; [...] if the resulting value is true, the value of the conditional-or expression is true and the right-hand operand expression is not evaluated. If the value of the left-hand operand is false, then the right-hand expression is evaluated; [...] the resulting value becomes the value of the conditional-or expression. 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.
A little repetitive, maybe, but the best confirmation of exactly how they work. Similarly the conditional operator (?:) only evaluates the appropriate 'half' (left half if the value is true, right half if it's false), allowing the use of expressions like:
int x = (y == null) ? 0 : y.getFoo();
without a NullPointerException.
Short circuit here means that the second condition won't be evaluated.
If ( A && B ) will result in short circuit if A is False.
If ( A && B ) will not result in short Circuit if A is True.
If ( A || B ) will result in short circuit if A is True.
If ( A || B ) will not result in short circuit if A is False.
No, if a is true (in a or test), b will not be tested, as the result of the test will always be true, whatever is the value of the b expression.
Make a simple test:
if (true || ((String) null).equals("foobar")) {
...
}
will not throw a NullPointerException!
No it won't, Java will short-circuit and stop evaluating once it knows the result.
Yes, the short-circuit evaluation for boolean expressions is the default behaviour in all the C-like family.
An interesting fact is that Java also uses the & and | as logic operands (they are overloaded, with int types they are the expected bitwise operations) to evaluate all the terms in the expression, which is also useful when you need the side-effects.
This goes back to the basic difference between & and &&, | and ||
BTW you perform the same tasks many times. Not sure if efficiency is an issue. You could remove some of the duplication.
Z z2 = partialHits.get(req_nr).get(z); // assuming a value cannout be null.
Z z3 = tmpmap.get(z); // assuming z3 cannot be null.
if(z2 == null || z2 < z3){
partialHits.get(z).put(z, z3);
}

Categories

Resources