Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Please explain this Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Please explain this" Watch "Please explain this" New topic

Please explain this

Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1090
I would like to know what the statement below means and what is the meaning of the word "invariant".
Whenever you would have written a comment that asserts an invariant.
Rajinder Yadav

Joined: May 13, 2003
Posts: 27
Was there something more to that line, hard to tell what context this statement was being used in?
The word 'invariant' means unchangable, something that is constant.

When faced with an easy thing to do and a hard thing to do, always pick the right thing to do!<p><a href="" target="_blank" rel="nofollow">Rajinder Yadav</a>
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1090
Hi Rajinder
Thanks Rajinder for the prompt reply. Sorry for the incomplete question. The question was asking about when should an assert statement be used. So we can say that final vars. and Strings are invariant.
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
An invariant is a rule that applies throughout the life of a data structure or procedure, and all changes must maintain the correctness of the invariant.
Say you have a program which is simulating some card game. You know that each pack of cards must have 13 cards of each suit, and that the suits are spades, hearts, diamonds and clubs. So, it is appropriate to use assertions which test the above, eg there cannot be more than 13 cards of the spade suit.

SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1090
Thanks Rajinder and Roger.
I agree. Here's the link:
subject: Please explain this
jQuery in Action, 3rd edition