Assertions in Java

posted in: Java | 0


Assertion is a statement in java that can be used to test our assumptions about the program. Each assertion contains a boolean expression and believed to be true while execution. If it fails, JVM will throw an error named “AssertionError”. Assertions are mainly used for testing purpose.

Advantage of Assertions:

  • Writing assertions while programming is one of the quickest and most effective ways to detect and correct bugs.
  • Assertions also serve to document the inner workings of your program by enhancing maintainability.

Syntax of using Assertions:

“assert” keyword is used to implement assertions in java.There are two ways to use assertions:

First way:


assert expression;
  • expression- boolean expression

When the system runs the assertion, it evaluates expression, and if it is false throws an  “AssertionError” with no detail message .

Second way:


assert expression1 : expression2;
  • Expression1 is a boolean expression.
  • Expression2 is an expression that has a value.

This version of the assert statement in assertion provides a detail message for the “AssertionError”. The system passes the value of Expression2 to the appropriate “AssertionError” constructor, which uses the string representation of the value as the error’s detail message.

Example of Assertion in java:

import java.util.Scanner;

class AssertEx{

public static void main( String args[] ){

Scanner scanner = new Scanner( );

System.out.print(“Enter your age “);

int age = scanner.nextInt();

assert age>=18:” Age is not valid”;

System.out.println(“value is “+value);




Enter your age 11

Exception in thread “main” java.lang.AssertionError: Age is not valid

Enabling and Disabling Assertion

By default, assertion are disabled at runtime. Two command-line switches allow you to selectively enable or disable assertion.

To enable assertion at various granularities, use the -enableassertions, or -ea, switch. To disable assertion at various granularities, use the -disableassertions, or -da, switch.

Example:    java -ea:package name

To enable assertion in all system classes, use a different switch: -enablesystemassertions or -esa. Similarly, to disable assertion in system classes, use -disablesystemassertions or -dsa.

Example:  java -esa -ea:package name 

Putting Assertion into Code

There are many situations where it is good to use assertion, including:

  • Internal Invariants
  • Control-Flow Invariants
  • Preconditions, Postconditions, and Class Invariants

Where not to use Assertion:

There are some situations where assertion should not be used. They are:

1.      Do not use assertions for argument checking in public methods.

According to Sun Specification, assertions should not be used to check arguments in the public methods because assertions should result in appropriate runtime exception e.g. IllegalArgumentException, NullPointerException etc.

2.      Do not use assertions, if you don’t want any error in any situation.

Because assertions may be disabled, programs must not assume that the boolean expression contained in the assertions will be evaluated. Violating this rule has dire consequences.



Leave a Reply