• 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

compile time constant expression

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone.


conditional expression in for loop is a compile time expression
so return in line9 will never get executed but compiler couldn't catch it
and program compiles successfully why???
In case of if also similar thing happens.
please explain reason for both...


thnks in advance.
 
Ranch Hand
Posts: 178
2
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The condition is checked compile time, but the value of the variable is not checked. That means, though you initialize the int with 1, compiler don't know whether the value of the variable. So there is only a possibility to run that code.
But the value of final variables are known by the compiler. So you get an error here.
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ramesh Pramuditha Rathnayake wrote:The condition is checked compile time, but the value of the variable is not checked. . . .

There is a good reason for that. Imagine how difficult it would be to write a compiler which can predict the runtime value of a variable
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sawan Mishra wrote:so return in line9 will never get executed but compiler couldn't catch it
and program compiles successfully why???


Simply put: Because the compiler doesn't check the body of the loop to see what might happen to i (although I believe there are a few exceptions to that rule).

Question for you: Would you want to have to write such a compiler? And if so, how would you go about it?

It could certainly be done on a limited basis, but only at the expense of a lot of stuff that really isn't the business of a compiler. Don't forget that a compiler is basically a syntax checker, not a checker for "well-formed" logic.

There are tools around checking if you're doing "anything stupid", but they're MUCH more involved than what the compiler does.

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote: . . . Would you want to have to write such a compiler? And if so, how would you go about it? . . .

And if you ever get it to work, will you live long enough to see it compile a large application ?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic