• 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

Question38 from Valentin Crettaz

 
hired gun
Posts: 250
MS IE Oracle Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The answer says that there are compilation errors on line 1 and line 4, but it compiled and ran fine for me.
Any clues?
[Code edited by Val]
[ March 18, 2003: Message edited by: Valentin Crettaz ]
 
John Hembree
hired gun
Posts: 250
MS IE Oracle Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for the post, found a similar thread on this here.
However it is interesting to note that it appears to compile and work fine from within JBuilder, but provides the errors on line 1 and 4 when compiled from DOS. Talk about confusing.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyway...
It depends with which compiler you compile the code, but as per the Java specification (JLS 14.20 Unreachable Statements), such code SHOULD NOT compile.
An excerpt of that section is given below:


[...]
the following statement results in a compile-time
error:

while (false) { x=3; }
because the statement x=3; is not reachable; but the superficially similar case:

if (false) { x=3; }
does not result in a compile-time error. An optimizing compiler may realize that the statement x=3; will never be executed and may choose to omit the code for that statement from the generated class file, but the statement x=3; is not regarded as "unreachable" in the technical sense specified here.



If you read that section entirely, it will become clear to you that only lines 2 and 3 compile correctly. Bottom line, when in doubt, trust the specification and not the compilers which are not always specification-compliant
[ March 18, 2003: Message edited by: Valentin Crettaz ]
reply
    Bookmark Topic Watch Topic
  • New Topic