• 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

Doubt in assert

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question goes like this::





What causes compilation to fail???

Answer is line 11.

Please explain how???



[BPSouther: Added code tags]
[ February 07, 2008: Message edited by: Ben Souther ]
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure. What is the compile-time error message that you are getting?
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler error is "unreachable statement". The asserts won't cause an unreachable statement compiler error, maybe because you could always run with assertions disabled. Things like code after and outside of a while(true) or code after an explicitly thrown Throwable will cause unreachable statement compiler errors. Test this yourself by replacing the while loop with a "throw new RuntimeException();" It's fun to try to cause the compiler to complain!
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah I think the reason is because of the infinitly loop as well. Because while(true) then it is no chance for the outsider assert to run for sure. Try to add a break conidition in the loop and see what happen. (I havent tried this but would be keen to know).
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For this code, you will get compilation error since compiler can easily predict the value for while loop and its constant value(i.e. true).

Following code works fine because, aFlag is a variable parameter holding boolean value. Compiler thinks "true" as a final boolean value, but for "aFlag" as a variable boolean value.

[CODE]
public void bar(){
boolean aFlag = true;
while(aFlag){
assert false;
}
assert false;
}
[CODE]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic