This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have David & Dawn Griffiths on-line!
See this thread for details.
Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

unreachable code

 
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
have a look at the these while() and for for() loop



if while() loop is giving error saying that x=5; is not reachable, then why not the same error is coming in the for() loop ??
 
Marshal
Posts: 8104
571
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Not 100% sure, but here is how I think in my head. Someone will correct me if I'm mistaken.

1. To "while" loop's condition check you provided compile time constant - actual literal value, which is known at compile time. So for the compiler it becomes clear, that code in a body of while loop will never execute.
2. In "for" loop "i < 0" "i" is not compile time constant - value being evaluated during runtime, so, value of "i" is not known at compile time, so compiler assumes it is all good to go.

If you were try "for (int i = 0; 0 < 0; i++)" you should get the same error message I think - "unreachable code". Give a try.

 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:If you were try "for (int i = 0; 0 < 0; i++)" you should get the same error message I think - "unreachable code". Give a try.


hi, yes it is giving the same error
but is it so?
why condition check of while() is done at compile time and that of for() loop at runtime ??
 
Liutauras Vilda
Marshal
Posts: 8104
571
Mac OS X VI Editor BSD Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Puspender Tanwar wrote:why condition check of while() is done at compile time and that of for() loop at runtime ??

Hi again. There is nothing special with "while" or "for" loops. They key thing is the value you supply them.

1. Once again:
unreachable code, because "false" is a compile time constant, in different words, literal value (false, true, 1, 2, 3, 4, null), which is known at compile time. The value for the compiler is known already during the compile time, so compiler knows, that it won't change and the code won't be reachable.

2. If you change code to:
Code is reachable, because "variable" is not compile time constant, not actual value, but variable which holds value, so compiler don't know what value going to be during the actual code execution moment.

3. But, if you declare variable as a constant, with keyword "final" it becomes compile time constant and will give you an error:
Again, it is because compiler knows, that value defined with keyword "final" cannot change during the code execution as it is constant.
 
Ranch Hand
Posts: 624
9
BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1 Thumbs up Vilda.
Learnt some interesting stuff.
 
Ranch Hand
Posts: 58
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Vilda,

Good insights.

It's interesting that in your second example, variable is effectively final. In the case of closures, the compiler allows the use of effectively final variables. You'd think, therefore that it'd be able, by the same logic, to determine that line 3 is unreachable.
 
Liutauras Vilda
Marshal
Posts: 8104
571
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, effectively final variable concept has been introduced in Java 8, if I'm not mistaken.
But this could be easily checked, write a program with 2nd code example (on machine where Java 8 runs) and you'll see what compiler says to you.
I'll be able to try that myself later on, I'm running on Java 7 at the moment where I am.
 
Gordon Brown
Ranch Hand
Posts: 58
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Indeed. Same issue in 8 though (have checked): it warns about unreachable code when the variable is final, but not when it's effectively final.
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote: There is nothing special with "while" or "for" loops. They key thing is the value you supply them.
unreachable code, because "false" is a compile time constant, in different words, literal value (false, true, 1, 2, 3, 4, null), which is known at compile time. The value for the compiler is known already during the compile time, so compiler knows, that it won't change and the code won't be reachable..


thank you for such a brief explanation, helped a lot .

Liutauras Vilda wrote:Well, effectively final variable concept has been introduced in Java 8, if I'm not mistaken.


yes it is a java 8 concept

Gordy Bone wrote:Indeed. Same issue in 8 though (have checked): it warns about unreachable code when the variable is final, but not when it's effectively final.


the reason for this could be that -> an effectively final variable couldn't be a compile time constant, as we can change that later in our code(unlike final variable), so compiler is not pretty much sure about the effectively final variable that whether it is constant or not and treats it like the normal variable
 
Liutauras Vilda
Marshal
Posts: 8104
571
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Puspender Tanwar wrote:thank you for such a brief explanation, helped a lot .

You're welcome
 
Gordon Brown
Ranch Hand
Posts: 58
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Puspender Tanwar wrote:
the reason for this could be that -> an effectively final variable couldn't be a compile time constant, as we can change that later in our code(unlike final variable), so compiler is not pretty much sure about the effectively final variable that whether it is constant or not and treats it like the normal variable



In order for a variable to be effectively final, the compiler needs to know that the variable is isn't reassigned a value after it's initialised.

I suspect that this is something that could be implemented (with respect to while loops) but has not yet been. If anyone knows for sure I'd be really interested to hear.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic