• 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 is this unreachable error

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check it.
public class Cjgreen{
public static void main(String argv[]){
Cjgreen c = new Cjgreen();
c.jgreen();

}
public void jgreen(){
int iNum =1 ;
while(iNum >0){

toffer:
for(int i = 0; i < 3; i ++){
continue toffer;
System.out.println(i);
}
}

iNum --;
}
}
above code gives compilation error , says that System.out.println(i); is
unreachable.
than check below code :

public class Hello
{
public static void main(String argv[])
{
final int i=1;
if(i>5)
System.out.println("test");
}

}

above works fine with no errors , but here also System.out.println("test"); is unreachable .

can anybody explain the concept of unreachable correctly
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler handles 'if' statements as a 'special case' when determining wether or not a statement is reachable. It does not test the condition. This allows programmers to set up 'flags' that can be used in debugging code.

14.21 Unreachable Statements


As an example, the following statement results in a compile-time error:

while (false) { x=3; }

because the statement x=3; is not reachable; but 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.

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you observe this code for any value of i this will not reach system.out.println(i); i.e if you change for loop to some thing like
(for int=0;i<b;i++) where b can be 3 or 100 or 1000 or maximum value of integer but it will still not reach system.out.println(i); statement.

for(int i = 0; i < 3; i ++){
continue toffer;
System.out.println(i);
}


But if you consider the below code:

final int i=1;
if(i>5)
System.out.println("test");
}

if you can change the value of i to some thing greater than six then there is a chance to reach system.out.println statement.

But in above case if you change the value of i to any thing even if for loop is modified to for(; it will not reach print statement. Hence it is giving unreachable error.
 
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

if case is unreachable then y case 2 is not unreachable.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic