Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Javatutorials Assert in Java

Related Tutorials


 
 

Share on Google+Share on Google+

Assert in Java

Advertisement
Here we have discussed assertion in Java in detail with its types, benefits, syntax and an example that will help beginners to understand its concept and use.

In this section we will discuss about what is assertion, how to use assert keyword and benefits of assertion in Java. We have also provided an example to help beginners understand the concept of assertion in Java. Assertion are assumption or predictions made at the beginning of the program so that to ensure the program is true, otherwise, it will not reach to that point and the program will be terminated and throw an AssertionException error.

Assertion is used to test assumptions in a program and make it valid by catching exceptions and logical errors. It makes make program more readable and user friendly. It validates data at run-time. Assertion contain a Boolean expression which is set to be true in the beginning of the program.

Assertion are of two types:

preconditions- invoked when a method is invoked
postconditions- invoked when a method is finished

Assertion can also be used in control flow, as invariants of a class.

Implementing Assertion in Java

Assertion is carried out in Java by using assert keyword. It can be expressed in two ways:

assert expression:

This statement specifies that if assertion expression becomes false, an AssertionError is thrown and program is terminated

assert expression1 : expression2 :

This statement means that the expression1 is evaluated first and if it is false then an AssertionError error with an additional message from expression2 is thrown.

Benefits of Assertion In Java:

  • Assertion in Java is used to review the code.
  • It makes debugging easier.
  • It helps to integrate unit testing as Junit testing.
  • It improves the quality of code.

Syntax:

assert statement1;

"statement1" is a Boolean expression, which is checked when program is executed. If it returns true, program runs without interruption. If statement1 returns false,, the program throw AssertionError and program gets terminated.

assert statement1:statement2;

"statement1" is an error message and statement2 is a Boolean expression. statement1 gives an error message if the statement 2 return false, then it is passed to the Assertion Error constructor of the respective error class. The constructor changes the value in statement1 into string format.

Example of Assertion in Java:

Here we have defined a public class 'Mark Assert Demo'. Inside it we have defined the static variable i.e. maximum marks to be 100, changes is another static variable, The main static ( ) method has assumption of maximum marks i.e. 40, if the marks come below to 40, the code will show you java.lang.AssertionError and display the Marks is below than 40 on the command prompt.

public class MarkAssertDemo
{
static float maximummarks=100;
static float changes(float mark)
{
maximummarks=maximummarks-mark;
System.out.println("The maximummark is:" + maximummarks);
return maximummarks;
}
public static void main(String args[])
{
float g;
for(int i=0;i<5;i++)
{
g=changes(15);
assert maximummarks>=40.00:"marks is below 40.";
}
}
}

Output:

C:\saurabh>javac -source 1.4 MarkAssertDemo.java

C:\saurabh>java -ea MarkAssertDemo

The maximummark is:85.0

The maximummark is:70.0

The maximummark is:55.0

The maximummark is:40.0

The maximummark is:25.0

Exception in thread "main" java.lang.AssertionError: marks is below 40.
at MarkAssertDemo.main(MarkAssertDemo.java:16)

Resource:

Advertisement

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: October 8, 2013

Related Tutorials

Discuss: Assert in Java  

Post your Comment


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