my dog learned polymorphism
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 Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assertions" Watch "Assertions" New topic


Piyumi Niroshani

Joined: May 17, 2006
Posts: 6
Want to know how assert keyword use in java simple programs?I belive yours answer may have simple programs?this is ugent to me.please.
Ashok George
Ranch Hand

Joined: Mar 25, 2005
Posts: 87
Piyumi ,

Assertions are special features added in java 1.4 onwards
by default assertions will be disabled u will have enable assertion when u compile the java file


The assertion statement has two forms. The first, simpler form is:
assert Expression1 ;
where Expression1 is a boolean expression. When the system runs the assertion, it evaluates Expression1 and if it is false throws an AssertionError with no detail message.
The second form of the assertion statement is:
assert Expression1 : Expression2 ;
�Expression1 is a boolean expression.
�Expression2 is an expression that has a value. (It cannot be an invocation of a method that is declared void.)
Use this version of the assert statement to provide a detail message for the AssertionError. The system passes the value of Expression2 to the appropriate AssertionError constructor, which uses the string representation of the value as the error's detail message.

hope this helps!!
usually this helps a lot while debugging ur code in big projects!!
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Piyumi Niroshani

Joined: May 17, 2006
Posts: 6
Dear George & Wise,
Thank u Very much for ur good help for me.Met us again ok.
I agree. Here's the link:
subject: Assertions
jQuery in Action, 3rd edition