*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assertions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assertions" Watch "Assertions" New topic
Author

Assertions

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477


with assertion enabled.the above programs gives 012.That part i got it.But with assertion disabled how it displays infinite times.When it comes to execute
while 3<3 condn it returns false.I thought with assertion disabled also it will come out of loop.Please clear my doubt.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Shiva Mohan:
...with assertion disabled how it displays infinite times. When it comes to execute while 3<3 condn it returns false...

With assertions disabled, the expression following "assert" is not evaluated. So in this code, i1 is never incremented.

It's not that i1<3 returns false when i1 is 3. It's that i1 remains at zero.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Try this with assertions enabled, and with assertions disabled...
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
Sir! Excellent coding explanation.Thank you very much.
Without enabled assertions,it displayed all x value 0.(but i don't know why that increment doesn't happen)
does that mean we used inappropriate use of assertions.
I mean "Do not use assert expression that can cause side effects".
Aum Tao
Ranch Hand

Joined: Feb 14, 2006
Posts: 210
.(but i don't know why that increment doesn't happen)
does that mean we used inappropriate use of assertions.
I mean "Do not use assert expression that can cause side effects".


Exactly. Use assertions only for evaluating conditions that you know should never arise. In other words, the assertion code should not modify the data and behaviour of your program in any way. The compiler will not complain, but it is a bad programming practice and misuse of assertions.


SCJP 1.4 85%
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Assertions
 
Similar Threads
Assertion Question from Dan's exam
Assertion problem
Assertion question
Strange Assertion Behaviour
Inserting single column values of database table into multiple columns of jtable and do comparison