• 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

Unreachable code

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was surprised to find out that the compiler is smart enough to know that an infinite while loop makes the code below it unreachable if the condition is the literal true or a final boolean variable set to true, but not a boolean variable that is set to true just before the loop. For example:
5. while(true);
6. doIt();
or
5. final boolean b = true;
6. while(b);
7. doIt();
will both fail to compile, while
5. boolean b = true;
6. while(b);
7. doIt();
is acceptable. I was also surprised that it does not know that a second assert false is unreachable, e.g. compiling this with assertions on:
5. assert false;
6. assert false;
does not complain about the second assert. Can anyone give me a concise rule about when the compiler is smart about unreachable code and when it is not, as opposed to just knowing the cases?
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
check out what the JLS says about it: (�14.20 Unreachable Statements).
I think this is ok because you can disable assertions -- and the code would be reachable:
5. assert false;
6. assert false;
[ April 22, 2003: Message edited by: Jessica Sant ]
 
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is SUN the only company that produces the compiler? I thought other companies made their own versions. If that is the case. then wouldn't the compilors actions and intelligence depend on how the compilor was coded?
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
correct -- other companies have developed compilers... But they still need to follow the same rules (i.e. the JLS) How else would code be portable across platforms and across JVMs??
 
reply
    Bookmark Topic Watch Topic
  • New Topic