I have this simple code block
int a = 10;
int b = 20;
int c = 30;
boolean result = a++ == 10 || b++ == 20 && c++ < 30;
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(result);
When I run this, the console is
11
20
30
true
This result tell me that only a++ == 10 is evaluated.
As I read from this https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html,
operator && have higher priority than || and should be evaluated first. In this book OCA Java SE 8 Programmer I Certification Guide, at twist 2.4, a similar example give the answer that all the logical operands will be evaluated. && is evaluated first and || is the last.
But the result give me an assumption that the priority is just used to group operands and operators.
From this a++ == 10 || b++ == 20 && c++ < 30 to this a++ == 10 || (b++ == 20 && c++ < 30) When expression is evaluated, it is short-circuited by the value true on the left of ||. So the expression on the right (the && operator) is not evaluated.
Because if the operator && is evaluated first, the value of b and c will be increased but it does not.
Am I correct? Please help.
The evaluation order without parenthesis is from left to right.
Since the first evaluation was true and because the operant is || the rest is not executing. (The same will happened if your first result was false and then you have an && operant).
The ++ operator executes always after the comparison execution.
(In C++ there are two different operators ++c and c++ then you can choose when the addition takes place)
Nice thoughts!!!
The tutorial page on operators only gives you an informal description of the operator precedence, when the java code is executed. The table shown on the page does not exists as such in the JLS. The execution/evaluation order depends on how the source code is parsed and how the parse tree is generated.
A code fragment as a || b && c (in this example code) will be parsed as an Expression:
Expression:
LambdaExpression
AssignmentExpression
In this case, it's an AssignmentExpression:
AssignmentExpression:
ConditionalExpression
Assignment
In this case, it's a ConditionalExpression:
ConditionalExpression:
ConditionalOrExpression
ConditionalOrExpression ? Expression : ConditionalExpression
ConditionalOrExpression ? Expression : LambdaExpression
In this case, it's a ConditionalOrExpression (the first line, not the one with the tenary operator):
ConditionalOrExpression:
ConditionalAndExpression
ConditionalOrExpression || ConditionalAndExpression
And here we will have it translated to the second line (ConditionalOrExpression || ConditionalAndExpression) as we have the "||" characters here (the actual || operator).
The JLS specifies that the left operand of the || operator is evaluated first by 15.7.1. Evaluate Left-Hand Operand First:
The left-hand operand of a binary operator appears to be fully evaluated before any part of the right-hand operand is evaluated.
It also specifies, that the || operator is "short-circuited", as defined in 15.24. Conditional-Or Operator ||:
The conditional-or operator || operator is like | (§15.22.2), but evaluates its right-hand operand only if the value of its left-hand operand is false.
In total this means that the left operand is evaluated (a++ == 10, which will increase the variable a) and because this comparison results in true, the right operand will NOT be evaluated. So neither b++ nor c++ will be evaluated in any way.
This result tell me that only a++ == 10 is evaluated.
Which is the result that the language definition dictates.
But the result give me an assumption that the priority is just used to
group operands and operators.
You are correct.
Related
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.
My doubt is about the precedence of short circuit operators.
Classic example of short circuit operator is below.
if(denom != 0 && num/denom > 10 )
Here usage of shorthand operator allows us to prevent division by zero error because num/denom is never executed.
Now My question is Java says '/' operator has higher precedence than '&&' , then how come left side of '&&' is evaluated before '/'.?
/ has higher precedence than >, which has a higher precedence than &&. So, the expression
a && b / c > 0
is the same as
a && (b / c) > 0
which is the same as
a && ((b / c) > 0)
The expression is then evaluated from left to right.
If a is false, then the entire expression is false, without even evaluating the second term.
If a is true, then b / c is first evaluated, and the result is compared to 0.
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 &
I know that the Bitwise operators &, | and ^ are EITHER bitwise operators OR logical operators ... depending on the types of the operands.
If the operands are integers, the operators are bitwise. If they are
booleans, then the operators are logical.
Then why there are Logical operators &&,|| and! ? I believe there will be some situations where we can use only logical operator, and so they are.
So, can anyone explain such a situation? Or any advantage over bitwise ops.
Operators && and || evaluates lazily. This means that only one side might be evaluated.
Operators & and | evaluates eagerly, this means that always both sides are evaluated.
It is very important when you expressions have side effects.
Examples
x = 0;
(x++ == 0) || (x++ == 1); // x is 1
x = 0;
(x++ == 0) | (x++ == 1); // x is 2
Logical operators &&,|| etc let you short circuit the logic.
1==1 || complexMethod(/*param*/)
complexMethod() will not execute.
1==1 | complexMethod(/*param*/)
complexMethod() will execute.
Short circuiting basically means the condition will be evaluated only up to where it is necessary and not beyond that.
Uses in Short-circuit evaluation like
Ex:
see &&
if(Condition1 && condition2){
}
and ||
if(Condition1 || condition2){
}
in these cases
in which the second argument is only executed or evaluated if the first argument does not suffice to determine the value of the expression:
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);
}