aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why is it so Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why is it so" Watch "Why is it so" New topic
Author

Why is it so

Jennifer Warren
Ranch Hand

Joined: Aug 24, 2001
Posts: 53
here is a problem;
if u do some thing like:
while(false){
System.out.println("aaaa");
}
Compiler complains about not being able to reach the statement.
But if you do some thing like this:
void aMethod(){
// some code.
return;
// some more code.
}
here the compiler is ok, why its not complaining about some satatement being unreachable.
thanx in advance.
Jennifer Warren.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
My compiler does not accept the second code excerpt
I run J2SE 1.4 beta 2 on Linux and I tried the following code

When compiling I get the following compiler error

HIH

------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2581

Originally posted by Jennifer Warren:
here is a problem;
But if you do some thing like this:
void aMethod(){
// some code.
return;
// some more code.
}
here the compiler is ok, why its not complaining about some satatement being unreachable.

This will *DEFINITELY* give comipler error provided return statement is not conditional, did you actually try doing this?
HTH,
- Manish
Jennifer Warren
Ranch Hand

Joined: Aug 24, 2001
Posts: 53
Sorry i forgot to mention one thing;
return;// not like this but
if(true)return;
once again sorry,
Jennifer.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
I think you mean
if(false)
JLS 14.20 explains this very clearly, here is the excerpt

the superficially similar case:
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.

HIH

------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
Jennifer Warren
Ranch Hand

Joined: Aug 24, 2001
Posts: 53
no I mean if(true)return;
and the statements after that would never be executed. If the compiler has enough information to figure out while(false){} and tells that the statements or statement in the body of while statement is not reachable. Why not it figures out in the if statement that it will always return and as in your code int i=0; will never be reached.
I do understand the use of boolean it was just that i feel that compiler should know this too.
thanx.
Jennifer.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
As I mentioned before, have a look at the JLS 14.20.
The way the compiler looks at an if-statement is different from the way it looks at while or for-statements.
This is mainly to support debugging flags as stated in the JLS excerpt above.
HIH

------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
More food for thought... Based on what Val says...


While this does not compile ...

But this code compiles ...

And this compiles too...(trivial case)

This basically concurs with Val's post...I just thought of posting some examples.
Hope this helps
Shyam
[This message has been edited by Shyamsundar Gururaj (edited October 25, 2001).]
Jennifer Warren
Ranch Hand

Joined: Aug 24, 2001
Posts: 53
thanks Shyam,
i was out of town and could not check it any earlier. But, I should say I did went through all and I still feel that compiler should have been privided with enough information to detect these too with the complain of un-reachanle statement.
Well, I guess I'll have to live with it.
regards to all.
Jennifer.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Jennifer...
The compiler can be really nasty regarding unreachable statements when it comes to return statements. I have a couple of programs in my computer at my Lab that demonstrate this...I'll post them with my comments as soon as I get there..Should be going there in around 3 hours..
Shyam
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Well, I've come to my lab now...Here's the dirty stuff regarding the dark connection between the return statement and the unreachable statement error...

Well...I told you this stuff would be nasty...
Cheers!
Shyam
[This message has been edited by Shyamsundar Gururaj (edited October 30, 2001).]
SaiPrasad Jukalkar
Ranch Hand

Joined: Feb 20, 2001
Posts: 79
Originally posted by Shyamsundar Gururaj:
[B]Well, I've come to my lab now...Here's the dirty stuff regarding the dark connection between the return statement and the unreachable statement error...

Well...I told you this stuff would be nasty...
Cheers!
Shyam
[This message has been edited by Shyamsundar Gururaj (edited October 30, 2001).][/B]


when i compiled some of the code from above discussion, i was getting compiler error.
getting error in the following code for "Unreachable statement."
public class returntest{
public static void main(String[] args){
returntest a = new returntest();
a.test();
}
void test(){
int x = 5;
try{
return; // Specifying a return here is OK and will not cause an error in **5**
}catch(Exception e)
{
System.out.println("In catch : " + x);
}
System.out.println(x); // **5**
}
}
i apologize if you have already discussed this before.
thanks in advance.
Saiprasad.


JSP
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
I didn't get an error. I am using JDK 1.3_01


SCJP2. Please Indent your code using UBB Code
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why is it so