Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: Others. No Comments on Assertions
0
(0)

In this article we will learn what are assertions. How to create and use assertions in Java programs and how to enable and disable assertions while running a Java program.

 

Assertions in Java

 

Definition: An assertion is a condition that should be true during the program execution. They are generally used to detect errors (testing) during development of software. They have no use after the code is released to the users. They encourage defensive programming.


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


 

Creating and Using Assertions:

 

Assertions can be created using the assert keyword. The general form of using assert keyword is as follows:

 

assert condition;

 

When the given condition becomes falseAssertionError is thrown by the Java run-time. The second form of assert is as follows:

 

assert condition : expr;

 

In the above syntax, expr can be any non-void value which will be passed on to the constructor of AssertionError and will be displayed as an error message.

 

Enabling and Disabling Assertions:

 

For enabling them we have to use the following syntax while executing a Java program:

 

java -ea ClassName

 

Where -ea denotes enable assertion and similarly for disabling them we can use the following syntax:

 

java -da ClassName

 

Where -da denotes disable assertion.

 

For enabling or disabling assertion in a package we can use the following syntax:

 

java [-ea | -da] [:package-name… | :ClassName]

 

Example Program:

 

Let’s consider a Java program where the numbers entered by the user must not be negative values. To implement this we can use assert keyword as follows:

import java.util.Scanner;
class AssertionDemo
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		System.out.println("Enter n: ");
		int n = input.nextInt();
		assert n>0;
		System.out.println("n = " + n);
	}
}

 

If the n value is given as -9, then output of the above program is:

 

Exception in thread “main” java.lang.AssertionError
at AssertionDemo.main(AssertionDemo.java:9)

 

As the above error message does not give much information we can use second form of assert as shown in the below program:

import java.util.Scanner;
class AssertionDemo
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		System.out.println("Enter n: ");
		int n = input.nextInt();
		assert n>0 : "n cannot be negative";
		System.out.println("n = " + n);
	}
}

 

Now the output of the above program for -9 as n value is:

 

Exception in thread “main” java.lang.AssertionError:

n cannot be negative
at AssertionDemo.main(AssertionDemo.java:9)

 

Use the following links for more information on assertions:

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

Your email address will not be published. Required fields are marked *