• Post Reply Bookmark Topic Watch Topic
  • New Topic

which has more priority: || and &&  RSS feed

 
david hu
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
which has more priority: || and && ?
 
Junilu Lacar
Sheriff
Posts: 11429
173
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
&& takes precedence over ||
That is,
a || b && c
will be evaluated as
a || (b && c)

Junilu
 
sun ram
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both will have the same priority. If both occur same line evaluate left to right.
import java.io.*;
public class AQuestion{
public static void main(String args[])
{
boolean a = true;
boolean b = false;
boolean c = true;

if( a || b && c ) {
System.out.println("Hello");
}
if( a && b && c ) {
System.out.println("Hello Hello");
}
}
}
You will get the output Hello
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sun is right; for the purposes of the exam, be clear on the difference between precedence and associativity of operators.
Associativity governs the order of execution when two operators share precedence and appear in the same expression. With the 'short-circuit' logical operators, associativity is left-to-right. This is the same for most, but not all, levels of precedence. Here's a table for guidance.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
Vaishali Joshi
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually sun is not right. I tested the following code:
boolean a = true, b = false, c = false;
System.out.println( a || b && c );
If || and && had the same precedence and if it were only left to right, the above line would have printed false , but it prints true. This means that it is equivalent to a || (b && c)
 
chen qing
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think sun is right. In your case, because a is true, no matter what value b or c is, the result will always be true.
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hm, well if we take Vaishali's example at its best intentions, he's not wrong either. We did not discuss the 'short-circuit' nature of the || and && operators.
'Short-circuit' behavior means the expression quits evaluatiing as soon as the end result of the expression is known. Consider these two examples:
if (false && .....)
if (true || .....)
In both cases, there is no need to evaluate the rest of the expression. Once a logical AND condition meets false, further processing isn't necessary. Same with any logical OR and a true. This situation does come up in the exam, so be ready for it.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
[This message has been edited by Michael Ernest (edited January 01, 2002).]
 
Junilu Lacar
Sheriff
Posts: 11429
173
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, let me make sure all the ducks are in the same row here...
When we talk about "priority", do we mean precedence or associativity? I just assumed that by "priority", the poster meant precedence.
The table that Michael referred to clearly shows that Logical AND has a higher precedence than Logical OR (and for that matter, same goes for the Bitwise AND vs. Bitwise OR)
Junilu
Originally posted by Michael Ernest:
Sun is right; for the purposes of the exam, be clear on the difference between precedence and associativity of operators.
...Here's a table for guidance.

 
chen qing
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right. I didn't notice that
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chen, for what it's worth, I thought you were right on the money. All I wanted to do was explain the principle behind your answer.
Junilu: we didn't say it plainly, but I hope it was apparent that "priority" is ambiguous in this context. "Precedence" and "associativity" are not, so those are the terms to use.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
[This message has been edited by Michael Ernest (edited January 02, 2002).]
 
david hu
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for all your replys for my post. Here let us talk about "short-circuit". I think Michael is wrong at the following point about "short-circuit".
---quoted from Michael's post---
if (false && .....)
if (true || .....)
In both cases, there is no need to evaluate the rest of the expression.
---------------------------------
See the following statements:
if (false && false ||true)
System.out.println(true); //actually evaluate true. So at least some expression after && is evaluated so we can get true.
Following is a example to make it clearer.
public class a {
public static void main(String[] args) {
if (ffalse() && ftrue() || ffalse())
System.out.println("The result is true");
}
static boolean ftrue(){
System.out.println(true);
return true;
}
static boolean ffalse(){
System.out.println(false);
return false;
}
}
The output of the above example is:
false
false
So the ftrue() is not evaluated, but the second ffalse() is evaluated. So if we have the statement like if (false && ...) some statement after && is evaluated, only the immediate variable/method after && is not evaluted.
As to the statement like if(true || ...), we can see all statement after || are not evaluated(So Michael is right at this point). I think it's the compiler's way to optimize the compiled code. From here I think we cannot see whether && or || has more precedence, because maybe the compile use this "short-circuit" experience to optimize the code. see the following example:

public class a {
public static void main(String[] args) {
if (ftrue() || ftrue() && ffalse() )
System.out.println("The result is true");
}
static boolean ftrue(){
System.out.println(true);
return true;
}
static boolean ffalse(){
System.out.println(false);
return false;
}
}
it will output:
true
The result is true
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if ( false && false || true ) {
System.out.println(true);
} //actually evaluate true. So at least some expression after && is evaluated so we can get true.

As Junilu noted above, && takes precedence over ||. Therefore this statement evaluates as:
( (false && false) || true ) --> ( false || true ) --> ( true )
The second false is "short-circuited." If the entire line were subject to associativity, it'd be a different matter.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
[This message has been edited by Michael Ernest (edited January 02, 2002).]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!