Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assertions

 
Vishal Arora
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the code given below. My question is that will it compile with assertions enabled.In my view it should not since statement //1 will be unreachable if asssertions are enabled. I tried to compile with assertions enabled and it compiled fine. But i am not able to undestand why.

[ August 03, 2005: Message edited by: Barry Gaunt ]
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Having Assertions enabled or disabled should never effect a program such that one line of code runs where another wouldn't based on it being enabled or not.

The compiler will not take those lines into account in determining if a line will run or not.

When an AssertionError occurs the application will stop, not continue on anyway, just like as if you issued a System.exit(0);

Mark
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you believe this should not compile with assertions enabled. Not an unreasonable assumption but that's never the case. Compile time expression values can only produce unreachable code in while, for and do statements. That way you can say if(SKIP_FLAG) ... and not have it compile in one state and not in the other. Not particularly orthogonal but useful nevertheless.



The rationale is that you can have a constant
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic