Using Assertions in Java

Wednesday Oct 29th 2014 by Joydip Kanjilal

Take advantage of assertions in Java to detect and fix bugs earlier in the SDLC process.

In the Java programming language, assertions (introduced in JDK 1.4) are statements that you can use to test your assumptions about the code. It is a nice way to detect errors in your code. When you execute assertions and the assertions fail, the runtime throws an AssertionError. The assert statement enables developers to write robust code and detect errors anywhere in the code. You can use the "assert" statements in Java source code to facilitate unit testing and debugging. Ideally, assertions are enabled at development time to detect and fix bugs in your code and disabled at deployment to increase application performance.

In this article, we will explore how assertions can be helpful to write efficient code and detect bugs early in the software development life cycle.

Assertions: a Deep Dive

You can use assertions to build robust code. "Java's new assertion mechanism, a welcome addition to the language now available in version 1.4, allows programmers to increase the robustness of their code by sprinkling it liberally with assert statements. The new assertion feature is easy to use, but any language feature, no matter how simple, can be used well or poorly." Reference:

Usage of assertion in Java ensures that, at a certain point in your code, the assertion or condition specified is true; otherwise, the instruction pointer would not have reached that point and an assertion error would have been thrown. In the Java programming language, assert is a debugging tool that will cause your code to throw an AssertionFailed exception if the condition specified fails. You can use assertions in Java in one of the following ways:

assert expression;
assert expression1 : expression2;

Here, Expression1 is a boolean expression and Expression2 is an expression that has a value.

The design goal is that assertions are used to check post-conditions and they should never fail pre-conditions. If you code is correct, it should never fail an assertion when it is triggered. The assert statement throws an AssertionError if you execute your code with assertion turned on.

int a = 12, d = 12;
assert a >= 0 && d <= 10;

The following code segment illustrates how you can use assertions in your code:

public class Sample {
   public static void main(String[] args) {

      int num = Integer.parseInt(args[0]);

      assert number <= 100;

      // The execution terminates if the value of num
      // is greater than 100

       System.out.println("The assertion passed...");


You should use the -ea or -enableassertions switch of Java To enable assertion. As an example, suppose you have a program written in Java stored in a file in the disk as Here is the sequence of statements you should execute in the command line to execute your program with assertion turned on.

java -ea Sample

Points to be Noted

Although assertions do help detect errors in your code, there are certain situation when you should avoid using them. Here is the list of such situations:

  • You should not use assertions to validate your business logic.
  • You should not use assertions to check arguments in public methods because it would otherwise throw runtime exceptions. In other words, IllegalArgumentException, NullPointerException, and so forth.
  • You should disable assertions before deployment.
  • Avoid using assertions to check or validate method arguments.
  • Note that assert statements will be evaluated only if assertion is enabled.


Here's a list of some great resources on this topic:


An assertion is a statement in Java that helps you to test your code. When the condition specified is not satisfied, an AssertionError will be thrown. You should enable assertions, though; else, the assert expression will not have any effect on your code. When an assertion fails, the assertion failures are labeled in the stack trace with the file and line number at which they were thrown. In this article, we discussed how assertions in Java can help to detect errors in your code. Happy reading!

Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved