Assertions in Java

Assertion

In a previous blog, we learned about the types of Exceptions along with their examples. If you want to learn more about the types of exceptions visit Types of Exceptions in Java . In this blog, we will learn about the Assertions in Java. Let’s get started.


Assertions

Assertion in Java is a statement that guarantees the correctness of the assumptions which we have made in the code. We must use the assert keyword to achieve the assertion. When an assertion is executed it must be true. If it fails then JVM throws an error called AssertionError. Assertions are mainly used for testing purpose.


Syntax of Assertion

Assertion is done with the help of the assert keyword. assert statement can be used in two ways:

First Way:

assert expression;

Second Way

assert expression1 : expression2;

e.g.

import java.util.Scanner;
class AssertionEx {
	public static void main(String args[])
	{
    	int value = 18;
    	assert value >= 20 : " The value is above 20 which is considered as overweight";
    	System.out.println("value is " + value);
	}
}

Output

value is 18

As we can see that the output is displayed without any error, this is because by default the assertions are disabled. After enabling the assertions we will get the following error:

Exception in thread "main" java.lang.AssertionError

Enabling Assertions

As we have been in the previous section, assertions are disabled by default. We have to run a command to enable it. The syntax to enable the assertions is as follows:

java -ea AssertionEx

OR

java -enableassertions AssertionEx

Disabling Assertions

In order to disable the assertions we have to use the following statement:

java -da AssertonEx

OR

java -disableassertions AssertionEx

Why to use Assertion

  • Unreachable code
  • While executing the program the code which does not execute is called unreachable code. We use assertions to make unreachable code as unreachable.

    e.g.

    void unreachableCode() {
      System.out.println("Executing the statements ");
      Return;
      System.out.println("Unreachable code");
      assert false;
    }
  • Documenting Assumptions
  • Many programmers use comments to document the assumptions.

    e.g.

    if (i % 4 == 0) {
        ...
    } else { // We are aware (i % 4 == 1)
        ...
    }

    In the above example rather than using comments, we can use assert. Because as the program grows it is possible that the comments can get out of date and out of sync. In this case we will have to use assert statements, otherwise they will fail even for the valid condition.

    e.g.

    if (i % 4 == 0) {
        ...
     } else {
         assert i % 4 == 1 : i;
         ...
     }
  • Assertion is also used to make sure that the default switch case is not reached.
  • e.g.

    switch (months) {
      case "January":
        System.out.println("January");
        break;
      case "February":
        System.out.println("February");
        break;
      case "March":
        System.out.println("March");
        break;
      case "April":
        System.out.println("April");
        break;
      case "May":
        System.out.println("May");
        break;
      case "June":
        System.out.println("June");
        break;
      case "July":
        System.out.println("July");
        break;
      case "August":
        System.out.println("August");
        break;
      case "September":
        System.out.println("September");
        break;
      case "October":
        System.out.println("October");
        break;
      case "November":
        System.out.println("November");
        break;
      case "December":
        System.out.println("December");
        break;
    }

    In the above example there is no default value which means that one of these 12 cases will always get executed. But if suppose there is a situation where due to some reason none of the cases is executed then the assumption will be false. This assumption should be verified using an assertion to make sure that the default switch case is not reached.

    e.g.

    default:
      assert false: month + " is invalid Month";

Where not to use Assertions

  • We should not use Assertions on command line arguments.
  • We should not use Assertions to replace error messages.
  • We should not use Assertions to check arguments in the public methods.
Get in Touch

Atrowel will be pleased to receive your feedback and suggestions. Those of you who would like to contribute, please send us an email.