• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What does this compile error mean?

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Test {
int y=32;
int a;

Test() {
this(6);
}
Test(int x) {
pause:
for ( int i=0; i < 2 ; i++) { <br /> if ( x > y >> 3 ) {
continue pause;
a = y*6; //line 14, compile error: Statement not reached
}
else {
a = y*9;
}
a++;
System.out.println(a);
} //end of for loop
} //end of Test(int x) constructor

public static void main(String [] args) {
Test t = new Test();
}
}
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bin, in this case the compiler message is actually pretty accurate.


line 1 will always send the program back to the for loop, thereby preventing the following line from ever being executed. The compiler checks to see if utterly unreachable statements exist in the code and flags them (since they can never be reached, why do they exist in the code at all?).
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bin,
It looks to me like the compiler is right. In your loop:
pause:
for ( int i=0; i < 2 ; i++) { <br /> if ( x > y >> 3 ) {
continue pause;
a = y*6; //line 14, compile error: Statement not reached
} else {
a = y*9;
}
if y right shifted 3(was this a typo?) is less than x then it continues the labeled loop. If it's not then it multiplies y * 9 and assigns the result to a. so regardless if the test is true or false it can not reach the line after the continue statement. The other thing is that through out the lopp none of the value in the if statement change, regardless of howmany times you go through the loop and increment i neither x nor y change value so the test result will always be the same.
I can't be sure exactly what you were going for here but I assume you want to put the a = y*6; as the line before your continue statement. And maybe the y is supposed to be an i?
hope that helps
Dave
 
Bin Wang
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
First of all,there is no typo in the program.
Test(int x) {
pause:
for ( int i=0; i < 2 ; i++) { <br /> if ( x > y >> 3 ) {
continue pause;
a = y*6;
}
else {
a = y*9;
}
a++;
System.out.println(a);
} //end of for loop
} //end of Test(int x) constructor

As X > y >> 3 always return ture, the else part of the if block will never get a chance to execute. But if I delete a=y*6; the program will compile and run, why the compile doesn't check it?
Is that true that the compile just check the unreachable statements in the if block?
 
Scott Appleton
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bin,
This is a bit of a subtle distinction, but the compiler plays dumb in this instance. It's not going to bother to do all the math and run a "simulation" to see whether the else block will be reached.
You and I (and Dave, as his post described) can figure out that the else won't be executed. You kind of have to hit the compiler upside the head with a shovel to make it realize that a piece of code cannot be reached. The "continue pause" statement placed immediately before another statement in your original code is egregious enough to make the compiler take note. If there is no statement following the "continue pause" within the if block then the compiler will not complain
The compiler will check the else block as well, and if you stick "continue pause" in front of the assignment statement in the else block you will get the Unreachable Statement error.

[This message has been edited by Scott Appleton (edited May 29, 2001).]
[This message has been edited by Scott Appleton (edited May 29, 2001).]
 
Bin Wang
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I got it. The word "utterly" is very important. The compiler will flag the utterly unreachable statement(s).
Thanks Scott and Dave.
 
Bin Wang
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In fact, x > y >> 3 will not always return true. it depends on what the value of x and y. I'm wrong in my previous previous message.
One thing I'm sure now is: the compiler will flag the UTTERLY unreachable statement(s).
 
Scott Appleton
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This being the JavaRanch, perhaps I should have said "udderly" unreachable statements"
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
uggghhh
Scott!! bad!!
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Test {
int y=32;
int a;
Test() {
this(6);
}
Test(int x) {
int a; //This I think, i guess every thing is right. but i dont have explanation for that.
pause:
for ( int i=0; i < 2 ; i++) { <br /> if ( x > y >> 3 ) {
continue pause;
a = y*6; //line 14, compile error: Statement not reached
}
else {
a = y*9;
}
a++;
System.out.println(a);
} //end of for loop
} //end of Test(int x) constructor
public static void main(String [] args) {
Test t = new Test();
}
}
 
reply
    Bookmark Topic Watch Topic
  • New Topic