For Programmers: Free Programming Magazines  


Home > Archive > Java Help > May 2006 > Short-circuiting code









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Short-circuiting code
crazy.marc@gmail.com

2006-05-18, 7:10 pm

High there, I dont completely understand what Short-circuiting is. The
way it is explained in Thinking in Java 3rd ed. doesn't really do it
for me. If someone could explain and maybe an example, that would be
greatly appreciated.

Thanks very much,
Marc

Casey Hawthorne

2006-05-18, 10:02 pm

crazy.marc@gmail.com wrote:

>High there, I dont completely understand what Short-circuiting is. The
>way it is explained in Thinking in Java 3rd ed. doesn't really do it
>for me. If someone could explain and maybe an example, that would be
>greatly appreciated.
>
>Thanks very much,
>Marc


Is that short-circuit evaluation of conditions?

OR

Dropping a copper penny on conductive ink?
--
Regards,
Casey
Chris Smith

2006-05-19, 4:10 am

<crazy.marc@gmail.com> wrote:
> High there, I dont completely understand what Short-circuiting is. The
> way it is explained in Thinking in Java 3rd ed. doesn't really do it
> for me. If someone could explain and maybe an example, that would be
> greatly appreciated.


Short-circuiting means that the second operand of certain conditional
operators will not be evaluated if the first is good enough to determine
the result. Specifically, it's used with && (logical AND) and ||
(logical OR).

For example, with the logical AND operator, I could start to say:

if (false && ...

and you could stop me there. It doesn't matter what's after the '&&',
because the expression will evaluate to false either way. The first
half is false, so it's impossible for both halves to be true.
Similarly,

if (true || ...

And I can stop there. The if statement will always be true, regardless
of the second operand to that OR statement. So the short circuiting of
logical operators just says that Java won't bother to evaluate the
second half of the && and || expressions above, because it doesn't need
to evaluate them. (Notice that if the first operand to && is true, or
the first operand to || is false, then it DOES need to evaluate the
second operand, and it will.)

Why does it matter? Because:

a) The second operand may have side-effects:

if (!skip && (i++ < 20)) ...

It is important, here, to realize that i will not be incremented
unless skip is set to false. If skip is true, then !skip is false,
and the && short-circuits to avoid evaluating the second half. So,
the increment of i never gets evaluated and it doesn't happen.

b) Or, the second half may fail. Short circuiting is the only reason
that the following code is valid.

if ((a == null) || a.equals(""))

Obviously, if a == null, then calling a.equals will fail with a
NullPointerException. Without short-curcuiting, that would happen
with the code above when a is null. Because of short curcuiting,
though, when a is null the second half is never evaluated, so there
is no chance of a NullPointerException.

Incidentally, case (a) may not be what you want. If that's true, there
are logical AND and OR operators that do not short-circuit: & and |. So
the difference between & versus && is that the second does short-circuit
while the first doesn't. Same for | versus ||. Most of the time, you
want to use the short-circuit versions.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
crazy.marc@gmail.com

2006-05-19, 10:02 pm

Thank you very very much for this, a very good explanation and I really
understand it now. I wasnt able to find anything that explained it too
well so you pulled me out of a rut =)

Thanks again,
Marc

Mitch

2006-05-22, 4:17 am

Chris Smith wrote:
[...]
>
> Why does it matter? Because:
>
>
> if ((a == null) || a.equals(""))
>

[...]

Thank you. I knew what it did, in the sense of the first example, and
only imagined it to be a neater (yet less clear, so I never used it) way
of performing such a statement. The second example though took me by
surprise, as a use I'd never seen before, and is a prime example of why
I lurk.

So yeah, thanks :)
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com