• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

The strange ways of compile time constants

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This, obviously, does not compile.


This compiles:


This, not so obviously, does not compile :


This DOES COMPILE :


Why ??!
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


there is a possibility to b can be false, so local variable x could not be initialize before it get use.


compiler make sure that x will get initialize here. so no error.


here after the exception, code cannot be reached.hence error.


[I am not sure]I think this is because of java compiler(javac) dont have pre-processor
 
Steli Niculescu
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I'm not sure either, actually my question was only for the last code sample.
I thought the compiler is sure that the exception will be thrown and the last statement will be unreachable, but it seems that's not true.
 
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It may be easy for a human being to spot that the last two code examples are identical, but it's not so easy for a compiler. You could make its analysis of which code will definitely be executed (or which won't) as complex as you want, and it will still miss opportunities for proving that some code can't be reached. It has to draw the line somewhere, and that line seems to be trying to prove the constant-ness of "if" condition values.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler allows this to build code like this:

It would be very annoying to get unreachable statement errors if Debug.enabled is set to false.
 
This tiny ad is suggesting that maybe she should go play in traffic.
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic