On Wednesday, 11/27/2002 at 09:36 EST, "Spencer W. Thomas"
<[EMAIL PROTECTED]> wrote:
> || is not "exclusive or".  It is just "inclusive or" with short-circuit
> evaluation -- it doesn't evaluate the second operand if the first is
> already true.

Bingo.  Similar for && vs. & -- operator && short-circuits (i.e. does not
evaluate the second operand if the first is false), while operator & does
not.

>  The result from (n < 0) || (n != 0) is EXACTLY THE SAME
> as the result from (n < 0) | (n != 0).  The first expression is slightly
> more efficient when executed, that's all.
 ...when n is negative, and the short-circuit "fires".

Predict the output of this little toy class (answer below)

public class X {
    public static void main( String[] args ) {
        Object o = null;
        try {
            if ( ( null == o ) || "foo".equals( o.toString() ) ) {
                System.out.println( "Now is the time " );
            }
            if ( ( null == o ) | "foo".equals( o.toString() ) ) {
                System.out.println( "...for all good men" );
            }
        } catch ( NullPointerException npe ) {
            System.out.println( "...to come to the aid of their country" );
        }
    }
}


Output:
   $ java X
   Now is the time
   ...to come to the aid of their country

Fun stuff, this.

-blair



____________________________________________________
To change your JDJList options, please visit:
http://www.sys-con.com/java/list.cfm

Be respectful! Clean up your posts before replying
____________________________________________________

Reply via email to