In Java, Assertion is a statement that ensures or tests the correctness of the assumptions made in a program. It is done with the help of the assert statement. When the written assumption is executed, it is considered as true. If it is false, an assertion error will be thrown by the Java Virtual Machine.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
The main reasons why Assertions are used are:
Syntax
Below is the syntax of the Java Assertion statement.
assert expression;
assert expr1 : expr2;
Any of these syntaxes can be used based on the requirement.
As already mentioned, assert can be written in two forms.
Even though these are the main pros of Assertion, there are certain situations where Assertions should not be used. They are:
Syntax
java –ea programname
Or
java –enable assertions programname
In Eclipse, it can be done using the below steps.
Step 1: Select Run Configurations.
Step 2: Go to the left panel and select Java Application, and Right-click on it.
Step 3: Select New configuration and type –ea on VM Arguments. Once it is done, click.
Similarly, assertions can be disabled using the syntax given below. Java –da programname
Now, let us see some sample programs for the assertion in order to get a clear idea of the same.
Java program to check whether a particular value is higher than 20.
Code:
class AssertionExample{ public static void main( String args[] ){ int val = 14; assert val>=20:" Value is not valid"; System.out.println("The given value is: "+ val); } }
Output:
Java program to check whether a particular user input value is higher than 20.
Code:
import java.util.Scanner; class AssertionExample{ public static void main( String args[] ){ Scanner <u>sc</u> = new Scanner( System.in ); System.out.print("Enter a number to check assertion "); //store the input value to the variable <u>val</u> int val = sc.nextInt(); //assertion check whether the input value is greater than 20 assert val>=20:" Value is not valid"; System.out.println("The given value is: "+ val); } }
Output:
Java program to check the number of days in a week.
Code:
class AssertionExample { //main method public static void main(String args[]) { //declare a string days String[] days = {" Monday " , " Holiday " , " Saturday " , " Tuesday " , " Wednesday " , " Sunday " , " Thursday " , " Friday " }; //set the assertion as 7 assert days.length==7 : "7 days are present in a week, Your input is wrong"; //print the line below System.out.println("There are " + days.length + " days in a week"); } }
Output:
Yes. The line gets printed as the assertion value satisfies the input value.
The following are the main advantages of using Assertion.
In addition to the above points, below are the important points that have to be known while studying Assertions.
Java Assertion is a statement that checks the trueness of a particular condition. It is commonly used in testing during software development. Moreover, they are used with certain Boolean expressions. In this article, several aspects such as syntax, working, pros, cons, and examples of Assertion is explained in detail.
The above is the detailed content of Java Assertion. For more information, please follow other related articles on the PHP Chinese website!