• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why is it so

 
Jennifer Warren
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Manish Hatwalne
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry i forgot to mention one thing;
return;// not like this but
if(true)return;
once again sorry,
Jennifer.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't get an error. I am using JDK 1.3_01
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic