• 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

compiler-time condition

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,




I wonder, why does it work in that way ? Sometimes compiler treats condition with if(true) like either 'compile-time' or not.
Thanks for any suggestions

Greets
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Difference in your example1 and example2 is that in the example1 you use a non-final boolean variable (b) so the compiler doesn't know whether the value of b will always be true, whereas example2 uses boolean literal "true" inside the if condition so the compiler knows it exactly. Both example3 & 4 share the same reason above.
 
Lukasz Wozniak
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, i understand that. But example 4 is the main point of my question : like you said Vijitha, compiler knows exactly what 'if(true)' is. So, in my opinion, it should be a compile error on line 19, because line is unreachable since we throw new exception. But line 19 compiles fine.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lukasz Wozniak wrote:Yes, i understand that. But example 4 is the main point of my question : like you said Vijitha, compiler knows exactly what 'if(true)' is. So, in my opinion, it should be a compile error on line 19, because line is unreachable since we throw new exception. But line 19 compiles fine.



The reasoning for example 1 and 2, is different for example 3 and 4. Example 1 and 2 is explained by Vijitha above.

As for example 3 and 4, the "unreachable code" checks are disabled for "if" conditions. The reasoning is to allow for conditional code. It is common practice to have something like this...



where "debugging" is a compile time constant. So, if the checks were not disabled, and debugging is off, you can have unreachable inside the "if" block.

Henry
 
Vijitha Kumara
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry Wong . I didn't know that.
 
Lukasz Wozniak
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All clear, thanks a lot!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic