File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Expression Evaluation with && and || operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Expression Evaluation with && and || operators" Watch "Expression Evaluation with && and || operators" New topic
Author

Expression Evaluation with && and || operators

Anant Jahagirdar
Greenhorn

Joined: Jan 09, 2003
Posts: 3
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

Joined: Mar 22, 2000
Posts: 12769
    
    5
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

Joined: Jan 09, 2003
Posts: 3
Hi
When the Precedence comes in to picure while using && and || operators?
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
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.


SCJP2. Please Indent your code using UBB Code
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
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

Joined: Jul 03, 2001
Posts: 2120
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

Joined: Jan 09, 2003
Posts: 3
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

Joined: Jan 10, 2003
Posts: 284
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 ]

giddee up
 
 
subject: Expression Evaluation with && and || operators