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



Home Java Java-exception Java Assertion

Related Tutorials


 
 

Share on Google+Share on Google+

Java Assertion

Advertisement
Assertion are simple check assumption made at the beginning of the program to ensure the program is true throughout provided by the Java language. For example, the range of age should be in between 18 and above; or cannot be more than 50.

Java Assertion

Assertion are simple check  assumption  made at the beginning of the program to ensure the program is true throughout  provided by  the Java language. For example, the range of age should be in between 18  and above; or cannot be more than 50.

These are the set of expression used in java that enables you to check the assumption made in a program during throughout the execution of program. An Assertion contain a Boolean expression which is set to be true in the beginning of the program. For Example ,a program has a method that allow the value being passed to it should not be zero and negative, you test this assert by sending a only positive number that is greater than zero by using assert statement in java.

Syntax used to declare Assertion

assert statement1;

The Syntax,statement1 is a Boolean  expression. When the program is executed  ,the statement1 in the assert is checked. If the statement1 returns true, assertion in program is true and the program run without interuption.Incase.,if the statement 1 returns false, the assertion made in the program fail, the program throw assertion error object and program will terminated and an assertion error object is thrown.

assert statement1:statement2;

An Statement is a error message and statement2 is a Boolean expression. the statement 1 gives you an error message that work only if there is an error in statement1.The statement 1 gives you an error, then it is passed to the Assertion Error constructor of the respective error class, if the statement 2 return false. The constructor changes the value in statement1 into string format and display the message on fail of assertion.

public AssertionError(char message)

The above  syntax  constructor Assertion Error create its object. The argument passed in the assertion error object contain the error  message that will display when an assertion fails.

How to Compile Assertion Files

The Assertion file is compiled with an option ,-source 1.4.The Syntax used in compilation of program is

Javac-source 1.4 AssertDemonstration.java

Where Assert Demonstration is the name of java program using assertion.

-source 1.4 is a command line option to make the compile to accept the code containing assertions.

How to Enable and Disable Assertion

Assertion are disabled at run-time during execution of java program .

The command line prompt  -ea or enable assertions is used to enable assertion during run-time execution of the program.

java -ea:AssertDemonstration

The command prompt -da or disable is used to disable assertion during run-time execution of the program

java -da :AssertDemonstration

Let Us Understand with Example

 In this Example We, defined a  public class 'Mark Assert Demo' Inside the class we define 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 on Command Prompt


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)
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: November 10, 2008

Related Tutorials

Discuss: Java Assertion  

Post your Comment


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