File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes unreachable statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "unreachable statement" Watch "unreachable statement" New topic
Author

unreachable statement

Tareq Ateik
Ranch Hand

Joined: Jan 16, 2010
Posts: 36

i dunno, whats the point of adding the unreachable statements to the compiler if they dont work sometimes.

we have that program:

this code wont compile because we have the unreachable line, so the second print statement wont execute

but in this program it works perfectly:

i just add the if statement, which will always be true, so the second print will never be executed. its like the first program but with if statement


i know that might be trivial, and not worth to start a new topic for it, but if the java engineers wanted to act smart they could apply it for the if statement case too.
besides, we never complained about that "unreachable statement" thingy on C++.


OCJA6, OCJP6
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4363
    
    8

Tariq Atiek wrote:besides, we never complained about that "unreachable statement" thingy on C++.

No. The compiler let us just go ahead and introduce errors into the code.
Tareq Ateik
Ranch Hand

Joined: Jan 16, 2010
Posts: 36
Matthew Brown wrote:
Tariq Atiek wrote:besides, we never complained about that "unreachable statement" thingy on C++.

No. The compiler let us just go ahead and introduce errors into the code.


i meant we never asked for that feature, and we were just fine without it. its not that big difference
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3446
    
  12
Tariq Atiek wrote:i meant we never asked for that feature, and we were just fine without it. its not that big difference


That's a matter of opinion. Having an unreachable statement is obviously a coding error (why would you write code that's never going to be executed) and hence the code may not do what you thought it was going to do. So if the compiler tells me about such a situation, then that's a good thing as it means I have to reexamine that code and make sure it is correct.

Joanne
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4363
    
    8

Java's designers generally took the approach that if the compiler can prevent errors, it should. And I'm glad they did. This is just one small example of the philosophy.

Checking for the case using an if statement is obviously a more complex problem. In most cases the condition will be an expression to be evaluated, which clearly can't be prevented by the compiler. They could have introduced a special case where the expression is a compile time constant, but there doesn't seem much point.

(Anyway, who is "we"? You sure nobody asked for it? Or do you mean "I"?)
Tareq Ateik
Ranch Hand

Joined: Jan 16, 2010
Posts: 36
yeah, i guess you are right.

well, i was frustrated because i was studying for the exam, and found it not a very important feature.
but i think its quite handy in big projects.
thanks
James Elsey
Ranch Hand

Joined: Dec 21, 2007
Posts: 228

If I understand correctly, the following will compile

if (true)
return;
System.out.println("Something");

It wont throw an unreachable code error because it is a valid if statement, even if the condition will always be met? Maybe it just checks that the if statement is legal, but not the condition to work out what would actually happen?


Kind Regards, James. OCPJP 1.6 || My SCJP / OCJCP Study Notes
Interested in : SCJP, Google App Engine, Stripes, Android;|| My Bite-Size SCJP Study Blog
Tareq Ateik
Ranch Hand

Joined: Jan 16, 2010
Posts: 36
James Elsey wrote:If I understand correctly, the following will compile

if (true)
return;
System.out.println("Something");

It wont throw an unreachable code error because it is a valid if statement, even if the condition will always be met? Maybe it just checks that the if statement is legal, but not the condition to work out what would actually happen?


exactly, the compiler will never check the condition of the if statement, so its not unreachable statements, and its totally legal.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: unreachable statement