execute the statements inside if statement without testing the if expression?

M

Matt

Someone told me in Java, we can execute the statements inside an
if-statement without evaluating the if expression.

i.e. if (expr)
{
stmt
}

I think about that, it seems doesn't make sense. I think in all
programming languages, it must execute the if expression. What do u
think? Please advise.
 
T

Tony Morris

Matt said:
Someone told me in Java, we can execute the statements inside an
if-statement without evaluating the if expression.

i.e. if (expr)
{
stmt
}

I think about that, it seems doesn't make sense. I think in all
programming languages, it must execute the if expression. What do u
think? Please advise.

That person was likely referring to the optimisations made by JIT (if they
weren't, they were lying).
If the expression is determined to always evaluate to true (by JIT), then
there is no need to evaluate the expression.
This optimisation has various 'gotchyas' in multi-threaded environments,
which are usually solved by using the "volatile" keyword.

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform
 
J

John C. Bollinger

Matt said:
Someone told me in Java, we can execute the statements inside an
if-statement without evaluating the if expression.

i.e. if (expr)
{
stmt
}

I think about that, it seems doesn't make sense. I think in all
programming languages, it must execute the if expression. What do u
think? Please advise.

The Java Language Specification (Sections 14.9.1 and 14.9.2)
unconditionally says that the expression is evaluated as part of the
execution of the if statement. In some cases, however, the Java or JIT
compiler might eliminate the if statement altogether if there is only
one possible result AND if evaluation of the expression cannot have any
side effects. As an example, some in this forum advocate simulating
conditional compilation in Java like this:

[...]

final static boolean DEBUG = <true or false>;

[...]

if (DEBUG) {
[conditionally compiled code here]
}

[...]

If DEBUG is assigned the value false then in principle the compiler
could omit the if statement and [conditionally compiled code] from the
bytecode altogether; otherwise it could omit the if statement itself but
include [conditionally compiled code] in the bytecode for the
immediately containing block. Note that DEBUG is final and static.
This optimization amounts to the compiler evaluating the expression
instead of making the VM do it at runtime. Neither Java nor JIT
compiler may omit the if statement if doing so can change any of the
results of executing the program.

It is also the case that Java provides short-circuit boolean operators
(&& and ||), which in some well-defined cases cause only part of a
boolean expression (the conditional expression of an if, for example) to
be evaluated. In this case potential side effects of the unevaluated
portion(s) are irrelevant, because the language spec (Sections 15.23 and
15.24) is explicit about which parts of the expression are evaluated in
which circumstances.


John Bollinger
(e-mail address removed)
 
D

Dale King

Matt said:
Someone told me in Java, we can execute the statements inside an
if-statement without evaluating the if expression.

i.e. if (expr)
{
stmt
}

I think about that, it seems doesn't make sense. I think in all
programming languages, it must execute the if expression. What do u
think? Please advise.

In addition to the constant expression explanation given by others, they may
have been referring to shortcut evaluation. In the following:

String s = null;
if( s != null && s.equals( "foo" ) )
{
stmt
}

In this case s.equals( "foo" ) will not be evaluated because the first
expression (s != null) is false therefore the entire expression will be
false regardless of what s.equals( "foo" ) returns. Java has both shortcut
versions of the boolean operators (&&, || ) and non-shortcut versions (&,
|).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top