assert 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.

Ads

Tutorials   
Java final method Assert in Java Final method in java Autoboxing in Java assert In Java JDBC batch insert using Java bean class Java Singleton Pattern Java Collection API - Java Tutorials Stack Overflow - Java Tutorials End of Year Puzzle The link to the outer class,java tutorial,java tutorials Orientating Components Right to Left,java newsletter,java,tutorial Non-virtual Methods in Java - java tutorials Multicasting in Java - java tutorials,tutorial Creation Time Comparison of Multi Dimensional Array- Java Tutorials Making Exceptions Unchecked - java tutorial,java tutorials Making Enumerations Iterable - JDK 5 Example Which is Faster - LinkedList or ArrayList? LinkedHashMap - Java Tutorials HashMap and HashCode JDK 1.4 the NullPointerException - Java Tutorials Java HashMap - Java Tutorials Java GC java awt package tutorial Java Multi Dimensions Array - Java Tutorials Java HashSet  - Java Tutorials Java Field Initialisation - Java Tutorials Java Comments - Java Tutorials J2EE Singleton Pattern - Design Pattern Tutorials Java - JDK Tutorials garbage collection in java Final Methods - Java Tutorials Exceptional Constructors - Java Tutorials Double-checked locking,java tutorials,java tutorial Disassembling Java Classes - Java Tutorials Java Deadlocks - Java Deadlocks Tutorials, Deadlocks in Java Commenting out your code - Java Tutorials Class names don't identify a class - Java Tutorials Causing Deadlocks in Swing Code Exception in Java - Java Tutorials Catching Exceptions in GUI Code - Java Tutorials boolean comparisons - tutorial Interthread Communication in Java BASIC Java - Java Tutorials Thread Deadlocks - Java Tutorials Autoboxing unboxing in Java - Java Tutorials Assertion in Java Appending Strings - Java Tutorials Anonymous Inner Classes - Anonymous Inner Classes tutorial Assertion in java
Ads

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

Share on Google+Share on Google+

assert In Java

Posted on: May 13, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: assert In Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads