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
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.
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.
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)
System.out.println("The maximummark is:" + maximummarks);
public static void main(String args)
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.
We have 1000s of tutorials on our website. Search Tutorials tutorials on our website.
Posted on: November 10, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles