Hi all, does anyone have any code that will prove java uses short circuit
evaluation e.g. (expr1 && expr2), if expr1 is false it will stop evaluating.
Thanks in advance
JS
VisionSet - 20 Nov 2005 23:12 GMT
> Hi all, does anyone have any code that will prove java uses short circuit
> evaluation e.g. (expr1 && expr2), if expr1 is false it will stop evaluating.
> Thanks in advance
public static void main(String[] args) throws DAOException {
int x = 1, y = 2;
boolean b, c;
b = true;
c = false;
if ((b = (x == 0)) && (c= (y == 2))) ;
System.out.println("b should be false: b= " + b);
System.out.println("c should be false: c= " + c);
b = true;
c = false;
if ((b = (x == 0)) & (c= (y == 2))) ;
System.out.println("b should be false: b= " + b);
System.out.println("c should be true: c= " + c);
}
Jimi Hullegård - 21 Nov 2005 01:15 GMT
> Hi all, does anyone have any code that will prove java uses short circuit
> evaluation e.g. (expr1 && expr2), if expr1 is false it will stop
> evaluating.
> Thanks in advance
public class NullTest
{
public static void main(String args[])
{
String a = "a";
String b = null;
if (a.equals("b") && b.equals("a"))
{
System.out.println("a == b??? and what about
NullPointerException??");
}
System.out.println("a != b :)");
}
}
If java whould evaluate both expressions in the if-statement, then it would
make a call to the equals-method of object b. But since b is null that would
cause an NullPointerException. If you run this program, you will see that no
exception is thrown. If you then change the a string to be "b" instead of
"a", then the first expression evaluates to true, and java evaluates the
second expression, resulting in a NullPointerException.
/Jimi
Roedy Green - 21 Nov 2005 02:58 GMT
On Sun, 20 Nov 2005 23:10:26 GMT, "JS#"
<james.sarjeant90@ntlworld.com> wrote, quoted or indirectly quoted
someone who said :
>Hi all, does anyone have any code that will prove java uses short circuit
>evaluation e.g. (expr1 && expr2), if expr1 is false it will stop evaluating.
>Thanks in advance
>JS
see http://mindprod.com/jgloss/multiposting.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
JS - 21 Nov 2005 09:52 GMT
Thanks for your help everyone. I was thinking way too deeply about it,
posted here then came up with the answer. Cheers
Roedy said:
> see http://mindprod.com/jgloss/multiposting.html
Yes I know, but I wasnt sure which group would respond. I dont normally
multipost but just this once.
sorry
Roedy Green - 21 Nov 2005 11:35 GMT
>> see http://mindprod.com/jgloss/multiposting.html
>Yes I know, but I wasnt sure which group would respond. I dont normally
>multipost but just this once.
even when you want to post to multiple groups, cross-post, don't
multi-post. See http://mindprod.com/jgloss/crossposting.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.