JavaBeat
calling cards | international calling cards | phone card
Search JavaBeat

Certification Kits
SCJP 1.5 Exam Questions
SCJP 1.6 Exam Questions
SCWCD 5.0 Exam Questions
SCBCD 5.0 Exam Questions
SCJP 1.5 Links
SCJP 1.5 Home
SCJP Books
SCWCD Books
SCBCD Books
New to SCJP?
SCJP 1.4 or SCJP 1.5?
Objectives
Articles
Mock Exams
Tutorials
Forums
SCJP Exam Voucher
Certification Links
SCJP 1.4
SCJP 1.5
SCJP 1.6
SCWCD 1.4
SCWCD 5.0
SCBCD 5.0
SCEA
SCEA 5.0
JavaBeat
Home
Articles
Tips
Code
QnA
Forums
350 Mock Questions on SCJP 1.5 - JUST Rs.200 or 7 USD
Send us mail to sales@javabeat.net
more details
1. Arrays 2. Variables Declarations 3. Constructors 4. Flow Control
5. Assertions 6. Java Keywords 7. Interfaces 8. Package
9. Finalization 10. Gargage Collection-1 11. Exception 12. Gargage Collection-2
13. Polymorphism 14. Encapsulation 15. Passing variables 16. Equals Method
17. Operators 18. Literals 19. Wrapper 20. String and StringBuffer
21. Math class 22. Threads 23. Thread Methods 24. Thread States
25. Thread Synchronization 26. Collections
Assertions page 2 of 5


An assertion is a statement containing a boolean expression that is assumed to be true when the statement is executed. The system reports an AssertionError if the expression evaluates to false. It is used for debugging purposes:


assert(a > 0); // throws an AssertionError if a <= 0

Assertions can be in two forms:

    
assert Expression1 ;
assert Expression1 : Expression2 ;

Expression1 should always result in a boolean value.

Expression2 can be anything that results in a value. The value is used to generate a String message that displays more debugging information. For instance:


class AssertionTest
{
int a ;
public void func()
{
assert (a < 0): "a is positive" ;
// Remaining code
}
}

In the above code, the assertion expression is true if a is negative. If a is positive, an AssertionError is thrown with the message "a is positive."

Assertions are disabled by default. To compile with assertions enabled, you need to use the source 1.4 flag:

	
javac -source 1.4 Test.java

To enable assertions at runtime, use the -enableassertions or -ea flags.

For selective disabling of assertions at runtime, use the -da or -disableassertions flags.

For enabling assertions in the system classes, use the -esa or -dsa flags. Assertions can also be enabled or disabled on a package basis.

Appropriate and inappropriate use of assertions
You can place an assertion at any location that you don't expect to be reached normally. Assertions can be used to validate the parameters passed to a private method. However, assertions should not be used to validate parameters passed to public methods because a public method must check its arguments regardless of whether assertions are enabled or not. However, you can test postconditions with assertions in both public and non-public methods. Also, assertions should not change the state of a program in any manner.

1. Arrays 2. Variables Declarations 3. Constructors 4. Flow Control
5. Assertions 6. Java Keywords 7. Interfaces 8. Package
9. Finalization 10. Gargage Collection-1 11. Exception 12. Gargage Collection-2
13. Polymorphism 14. Encapsulation 15. Passing variables 16. Equals Method
17. Operators 18. Literals 19. Wrapper 20. String and StringBuffer
21. Math class 22. Threads 23. Thread Methods 24. Thread States
25. Thread Synchronization 26. Collections
350 Mock Questions on SCJP 1.5 - JUST Rs.200 or 7 USD
Send us mail to sales@javabeat.net
more details

Favorites
AffiliatedAds.com
Buy movies
Access Control
Busby seo challenge contest
Sohbet
Chat
Webmaster Hosting Forum
Java Jobs
MyVideoLib
India News
Internet Advances
Sohbet
chat
Latest QnA
SCJD Tips
When we start a thread by applying start() method on it ,how does it knows that to execute run()method?
About Wrapper class in Java
How to configure weblogic 7.0 in MyEclipse?
Static Block and Static Initializer in Java

JavaBeat Website (2004-2008), India
javabeat | about us | planetoss
Copyright (2004 - 2008), JavaBeat