Page 1 of 1

About Question com.enthuware.ets.scjp.v6.2.467 :

Posted: Fri Feb 25, 2011 6:27 pm
by ETS User
Which of the following code snippets will compile without any errors?

Answer 1: while (false) { x=3; }
Explanation
while (false) { x=3; } is a compile-time error because the statement x=3; is not reachable;
I get a compile time error that states "x cannot be resolved". I do not get any compile time message stating the code is unreachable.
when I add the following line of code before this:

Code: Select all

int x;
I get no compile time errors at all.

Re: About Question com.enthuware.ets.scjp.v6.2.467 :

Posted: Fri Feb 25, 2011 9:12 pm
by Guest
Never mind.... I got that compiler error. There was another compiler error having to do with a duplicate definition of "x" in my code that was being flagged first by the compiler....

Re: About Question com.enthuware.ets.scjp.v6.2.467 :

Posted: Wed Mar 16, 2011 3:01 pm
by Guest
The explanation says that "for( int i = 0; false; i++) x = 3; is also a compile time error because x= 3 is unreachable.", so, why the option is marked as correct (whith no compile errors).

Re: About Question com.enthuware.ets.scjp.v6.2.467 :

Posted: Thu Mar 17, 2011 6:00 am
by admin
Hello, The option that is marked as correct is for( int i = 0; i< 0; i++) x = 3;

The statement given in the explanation is talking about a similar but different situation.

Re: About Question com.enthuware.ets.scjp.v6.2.467 :

Posted: Sat Dec 19, 2020 2:21 pm
by borkutip
Hello,

I think, that the explanation is not very clear, especially the connection between
for( int i = 0; i< 0; i++) x = 3;
and
for( int i = 0; false; i++) x = 3;

is not obvious (for me).

I think, that the clue is that the compiler checks only compile-time constants:
for example:
This is OK:
boolean b = false;
for (int i = 0; b; i++) {x = 3;};
and this is unreachable code:
final boolean b = false;
for (int i = 0; b; i++) {x = 3;};


So, here:
for( int i = 0; i< 0; i++) x = 3;
because "i" is not a compile-time constant, the compiler will not check (or can not see?) unreachable code (however it is obvious for a human-eye to
detect unreachable code)

Re: About Question com.enthuware.ets.scjp.v6.2.467 :

Posted: Sun Dec 20, 2020 11:03 pm
by admin
Enhanced the explanation.
thank you for your feedback!

Re: About Question com.enthuware.ets.scjp.v6.2.467 :

Posted: Wed Aug 09, 2023 11:34 am
by KevinGombe
How is it that if (false) { f=3; } compiles fine but the statement f=3 is unreachable

but the second statement :
while(false){
f=4;
}
rightfully doesn't compile for the same reason

Re: About Question com.enthuware.ets.scjp.v6.2.467 :

Posted: Wed Aug 09, 2023 8:08 pm
by admin
Technically, both contain unreachable statements and should not compile but the first one i.e. if(false) { } is made an exception. It is allowed explicitly by Java specification to allow conditional compilation, which is useful for writing log statements such as:

if(LOG_ENABLED) {
out.println("logging...");
}

Which book are you studying for the exam? This is explained in OCAJP Fundamentals by Hanumant Deshmukh and also in the explanation.