Home Javatutorials assert In Java
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

assert In Java

Advertisement
This section describes you how to implement assertion in Java. In this tutorial you will learn about what is assertion, how to express assert keyword in Java, where to use assertion in Java, benefits of implementing assertion in Java.

assert In Java

In this section we will discuss about the assert keyword in Java.

This section describes you how to implement assertion in Java. In this tutorial you will learn about what is assertion, how to express assert keyword in Java, where to use assertion in Java, benefits of implementing assertion in Java.

What is assertion ?

Assertion, in computer programming, specifies the true-false statement. This statement is called a predicate and is placed in the program that shows a developer is predicting the predicate is always true there. Assertion helps in to specify the program and explains why program is correct. Function of assertion can be stated as preconditions, postconditions, and invariants of a class.

Where To Use Assertions

Assertion is used in computer programming to make program more readable, user friendly and understanding. We can use assertion in the states as preconditions, postconditions. As well as assertion can also be used in control flow, as invariants of a class.

Assertion In Java

Assertion is also allowed in Java. Java allowed assertion for its features that it have. To use assertion in Java there is a keyword named assert. Assertion in Java validate data at run-time. Assertion in Java is introduced since JDK 1.4.

How to implement assertion in Java ?

Assertion in Java can be implemented using assert keyword in Java. In Java assert keyword can be used/expressed in two ways :

  1. assert expression : When you use such statement to implement assertion in Java it means that the assertion expression will be evaluated and if the expression becomes false then an AssertionError error will be thrown and the program will be terminated.
     
  2. assert expression1 : expression2 : When you use such statement to implement assertion in Java it means that the expression1 will be evaluated and if the expression becomes false then an AssertionError error with additional message (expression2) will be thrown by Java.

Benefits Of Implementing assertion In Java

There are various benefits of using assertion in Java. Use of assertion in Java code is a good programming practice that helps you in the review of your code. An assert keyword is used to implement the assertion in Java that offers various benefits as :

  •  Using assertion you can validate your data at run-time.
  • In Java you can make sure your assumption for a certain point on function would be true otherwise, it will not reach to that point and the program will be terminated and throw an AssertionException error.
  • Use of assertion in Java makes debugging easier that helps to detect bug in early phase of development.
  • Assertion using assert keyword in Java helps to integrate unit testing as Junit testing.
  • Use of assert keyword in Java improves the quality of code.

For assert keyword example you can go through the link : http://www.roseindia.net/javatutorials/use-assertion-in-java.shtml.

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: May 13, 2013

Related Tutorials

Ask Questions?    Discuss: assert In Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com