Please observe the following code... class forloop5 { public static void main(String args[]) { for(int x=0;false;x++) { System.out.println("hi"); } } } When I compile the above code, it is giving me the error: unreachable statement, ( as expected!! due to false in the forloop...) But, whereas the below code works fine... class forloop5 { public static void main(String args[]) { for(int x=0;false;x++) { //System.out.println("hi"); } } }
Whats happening here.. Why just {} is not giving any problem... Do forloops and {} have any deal with each other Can anybody throw some light on this??
It has nothing to do with the {} at all. The "unreachable statement" error message should provide a clue. It's telling you that there is a statement that has no chance of being executed. When you comment out the statement, the problem goes away.
Reachability is determined at compile time. In the second case, there is not statement to reach to (atleast not in the for loop) commented statements are not counted and hence you do not get any errors.
That's odd, I get compile errors in both examples. BTW, I also saw that problem in Valentin's Mock Exam. It says there that there are errors in these codes:
[ June 16, 2002: Message edited by: Paul Villangca ]
Forte gives compiler errors for both as well. However, the following does compile.
[ June 17, 2002: Message edited by: Charles Earwood ]
Paul Villangca
Ranch Hand
Joined: Jun 04, 2002
Posts: 133
posted
0
I think this is an exception to the unreachable statements rule - it's for debugging purposes.
manasa teja
Ranch Hand
Joined: May 27, 2002
Posts: 325
posted
0
Paul, I agree with you. The problem of "unreachable statement" comes only in case of loops. But, do not know the exact reason , why loops only bother about the statement , which is unreachable Can anybody look into this please?? ( With this message, I have become a ranchhand!! A pramotion to me ) [ June 17, 2002: Message edited by: Murthy Ram ]
Unreachable statements are not only possible with loops but also with other constructs, like return and so on... None of the following programs compile:
And so on... Unreachable really means that there is no way for a statement to be reached during the execution... As for the if(false), JLS 14.20 Unreachable Statements clearly states that:
if (false) { x=3; } does not result in a compile-time error. An optimizing compiler may realize that the statement x=3; will never be executed and may choose to omit the code for that statement from the generated class file, but the statement x=3; is not regarded as "unreachable" in the technical sense specified here. The rationale for this differing treatment is to allow programmers to define "flag variables" such as: static final boolean DEBUG = false; and then write code such as: if (DEBUG) { x=3; } The idea is that it should be possible to change the value of DEBUG from false to true or from true to false and then compile the code correctly with no other changes to the program text.
[ June 18, 2002: Message edited by: Valentin Crettaz ]
gives a compile error because anything in the for block is unreacheable, even though there isn�t anything, right?
Francisco
Francisco<br />SCJP<br />please use the [code][/code] tags when showing code.Click <a href="http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page" target="_blank" rel="nofollow">here</a> to see an example.
Compiling with v.1.3.1 does not give any errors. What version are you using that give compile errors?
manasa teja
Ranch Hand
Joined: May 27, 2002
Posts: 325
posted
0
Originally posted by Francisco A Guimaraes: So, just to clarify, the code:
gives a compile error because anything in the for block is unreacheable, even though there isn�t anything, right? Francisco
Francisco, The above does not gve you any compilation error. If you remove the comments for System.out.println("hi"); then, and it will give the compile error as the statement is unreachable. The moral of the story is {} and { // System.out.println("hi"); } are one and the same and will not any give any compilation errors.
Paul Villangca
Ranch Hand
Joined: Jun 04, 2002
Posts: 133
posted
0
I have to disagree with you on this one, Murphy. It doesn't compile under Forte, and it doesn't compile in the command line (1.4) either. Update: Well, whaddaya know. It does compile under 1.3.1, heh. Now which one should we believe? I stick with my statement, though (compile error.) [ June 19, 2002: Message edited by: Paul Villangca ]
manasa teja
Ranch Hand
Joined: May 27, 2002
Posts: 325
posted
0
I have not verified with hgher version. I am using 1.2 only.. in my previous post, I just answered Francisco's question. thanks murthy
Francisco A Guimaraes
Ranch Hand
Joined: Mar 20, 2002
Posts: 182
posted
0
i used jikes, with the jdk1.4 in the classpath, and 1.4 javac.
Francisco A Guimaraes
Ranch Hand
Joined: Mar 20, 2002
Posts: 182
posted
0
I just thought of something: if i�m taking the 310-025 exam(the actual SCPJ2), I would have to say the above code would compile and run? Francisco
I don't think they are so devious to ask such a question. This from JLS 14.20 may be helpful
The contained statement is reachable iff the for statement is reachable and the condition expression is not a constant expression whose value is false.
It seems this does unreacheable even no statement. However if different compilers gives different results, it is likely they don't ask such a question.
SCJP2. Please Indent your code using UBB Code
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.