JS# 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
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