File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

unreachable code

 
Allen Bandela
Ranch Hand
Posts: 128
Eclipse IDE MS IE Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is it that when I write the following code,
1. do{
2. //do something
3. }while(true);
4. //some code
I get a compiler error at line 4 saying "unreachable code",
whereas when I write the following code

1. int c=10;
2. do{
3. //do something
4. }while(c==10);
5. //some code

I don't get the unreachable code compile time error. At both times the condition in the while is "true".
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I could only guess that it is because c==10 is only evaluated at runtime whereas true is always true, so the compiler is able to tell you at compile time of your error.
 
Justin Fox
Ranch Hand
Posts: 802
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, you have C initialize before the loop, so the compiler figures,
maybe somewhere you'll be able to change the C to 9 or 8.

but when all you have is while(true);

the true can and will never be false.

so the compiler recognizes and throws this error..

try to put...

boolean found = true; before the loop, and then while(found);

at the end of the do loop;

see if it compiles then,

but you'll still crash at runtime...

Justin
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Allen,


yes it is a matter of compile time and runtime. The following code will not compile at the lonely semikolon at the end (representing code):

and it does not compile, because the compiler knows, that between declaration /initialization of the boolean variable condi and the while condition test (// in beteween), condi cannot change since it is final.
So, condi is a compile time constant and always true, and the semikolon is unreacheable code.

If you delete the final keyword, the code compiles, as condi now could have changed its value // in between, which the compiler cannot know.
And if you use the boolean literal true in the while condition, as in your first example, this true is also a compile time constant.

You can test it by making the int-variable final in your second example.



Yours,
Bu.
 
Allen Bandela
Ranch Hand
Posts: 128
Eclipse IDE MS IE Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all. So, can I safely conclude that all final variables are compile time constants? i.e they are assigned at compile time? and all non-final variables are not resolved at compile time?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Allen]: So, can I safely conclude that all final variables are compile time constants? i.e they are assigned at compile time?

No, being final is necessary by not sufficient to be a compile-time constant. For a variable to be a compile-time constant (a constant variable):
  • it must be final
  • it must be a primitive type, or a String
  • it must be initalized (at declaration) using a compile-time constant expression

  • The rules for compile-time expressions are a bit complex - follow the link to get the precise rules. But basically it means constant variable, literal, or an expression combining other constant variables or literals. So if something "non-constant" occurs anywhere in the expression, the whole expression is not a constant.

    [Allen]: and all non-final variables are not resolved at compile time?

    That part is true.
     
    Mahendra Shelke
    Greenhorn
    Posts: 20
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    boolean f = true;

    do
    {
    System.out.println("True");
    }
    while (c==10);

    System.out.println("False");


    In the above case i dont get any unreachable code error ..
     
    Mahendra Shelke
    Greenhorn
    Posts: 20
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sorry for the trouble guys .. got it ..
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic