• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Expression Evaluation with && and || operators

 
Anant Jahagirdar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am getting confused with the following example. Eventhough && has higher precedence the expression is evaluated differently. Please explain me.
class Test {
static boolean ant(){
System.out.println("within method");
return true;
}
public static void main(String[] args) {
boolean a , b, c;
a=b=c=false;
System.out.println((a=true) || (b= ant()) && (c=true));
System.out.println(a+ "," + b+"," +c);
}
}
out put is : true
true, false, false
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your clause (a = true) sets variable a to true and returns true. The || (short circuit or) sees true and does not evaluate the remainder. Remember, left to right evaluation.
Bill
 
Anant Jahagirdar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
When the Precedence comes in to picure while using && and || operators?
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Anant.
I have not been able to observe the evaluation of the right operand to && before its left one in any example I made up. Otherwise the short-circuit operators could not short-circuit.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The short circuit behavior of && is to quit if the left operand is false - since the result can't possibly be anything but false. Therefore to see the right operand evaluated you must have a true result on the left.
Bill
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anant, this code shows, at least, that the && operator has a bigger precedence than =

a) v is evaluated to false
b) true is evaluated to true
c) the precedence of = is compared with &&. Thus = is not carried out but instead...
d) m() is evaluated and false is printed
e) && is performed yielding true
f) now the previously obtained true is assigned to v and printed.
[ January 10, 2003: Message edited by: Jose Botella ]
 
Anant Jahagirdar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for your detailed answers. When short circuit operators are in the expression, the left operand is evaluated first and then if operator is || and evalated operand is true, the expression evaluation process stops. same thing happens for && with false operand.Is this correct?
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that sounds right to me Anant! From what i can tell, && and || are just looking for that short cut. a quick way out. the short circuit. As soon as they see something that says "this aint gonna work" (a false value in the values being evaluated by &&) or "this definately will work" (a true value in the case of ||), then boom, everything is dropped.
[ January 14, 2003: Message edited by: Jasper Vader ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic