Javatpoint Logo
Javatpoint Logo

Java Assert Examples

In this section, we will be able to learn the Assertion operation in Java and its corresponding examples. We will also learn the types of assertions that take place accordingly.

What is Assertion in Java?

The keyword "assert" performs an assertion operation in Java. The concept of Assertion allows the programmer to verify the assumptions that are taken during the execution of the program. In other words, the process allows checking and verifying the quality and correctness of the taken assumptions that are actually defined within the program. The keyword " assert " was introduced and started from the Java 1.4 version. In the initial times of the invention of the Java programming language itself, the keyword is brought up in the language.

The concept of Assertion is mainly useful for people who generally develop software rather than testing. So, we can say that this concept plays a very important and crucial role for developers. For every execution of Assertion, the developer expects it to be true. In other cases, if the Assertion fails and returns false, then the compiler shows an error representing the " AssertionError ". The keyword " assert " also makes the code more readable and helps in code optimization. The " assert " keyword is generally assigned with a boolean expression which can be either false or true as the intention of the Assertion operation matches the boolean.

The syntax for declaring and using assert keyword (type 1):

The assert keyword can be used in two different ways, which are already discussed above. And the second way deals with two expressions where both are declared with the assert keyword in such a way that the expressions are related to each other.

The syntax for declaring and using assert keyword (type 2):

How can we enable and use Assertion in Java?

In order to use Assertion in Java, we need not import any packages or use any libraries as everything runs with just a single keyword, i.e., " assert ". So, if we want to use and enable assertions in Java, we have to use a command line argument that promotes and enables the keyword " assert ". The command line argument is " -ea " or " -enableassertions" in which the first command line argument is the shorter form of the second command line argument.

After every execution, the assertion operation will be disabled by default, and the process of disabling the keyword is done by the JVM, i.e., Java Virtual Machine. This is done in order to have prior protection from the naming clashes that arise with newer JVM versions. So, before leaving, the JVM automatically disables the assertion validation so that it will not cause any problems during the execution of the next program.

Assertions can be enabled for the specified classes and packages also. For example, let us consider that we have a program containing the main class " Demo " with the file name "Demo". In order to enable the Assertion only for the program containing the class Demo, we can enable it by using the command line argument. Similarly, the assertions can be disabled by using the command line argument " -da " or " - dissableassertions ". Specified classes, packages, and files can also be restricted from using the assertions by disabling the assertions for those specified classes, files, packages, etc. In this way, the enabling and disabling can be done for the Assertion in Java.

Command line argument to enable Assertion in Java:


Command line argument to enable Assertion for a particular class or a particular file in Java:


Command line argument to disable Assertion in Java:


Command line argument to disable Assertion for a particular class or a particular file in Java:


Why should we use Assertions in Java?

For any task to be handled and executed perfectly, the programmer must know and must be pretty sure about his assumptions made within the entire program. If all the assumptions are wrong, the entire program turns out-graded. So, to make this certainly happen, the assertions must be used in various parts of a program.

AssertionError Handling:

The AssertionError is a type of unchecked error. So, the methods that use assertions particularly do not require declaring them, and further calling code should not try to catch an AssertionError. As the AssertionError is an unchecked error, its class extends Error, which extends Throwable. Unlike different exceptions in a program, assertions are usually handled at run-time. These exceptions are meant to indicate unrecoverable conditions. So, recovery or handling of AssertionError must never be attempted.

Points to note while using Assertion:

An Assertion can be disabled by the programmer. We should never assume that they will be included during the execution of a program. So, a programmer must keep certain things in mind while using assertions in their code. They are,

  1. The null values and empty optionals must always be checked while using assertions.
  2. Make use of unchecked exceptions such as IllegalArgumentException or NullPointerException to check input into a public method instead of using assertions.
  3. Assertions can be used in places where the code written will never be executed, such as the default case of a switch statement or after a never-ending loop.
  4. Do not call methods in assertion conditions. Assign the result gained from a method to a local variable and use that variable with the assert keyword.
  5. An assertion can be used to check conditions at the beginning of a method.
  6. The private arguments provided in the developer's code should not be checked using assert as the developer himself may want to check his assumptions about arguments.
  7. An assertion should not be used on command line arguments of a program.

A Java program to demonstrate Assertion:


Age of the person is 15

In the above program, we used the assert keyword on two expressions, i.e., 18 and " you cannot have a license ". Hence, the Assertion checks if our assumption is true or false by comparing the related expressions. Also, assert is not included in the execution, as we discussed above. In the end, we got our expected output by determining the person's age as 15.


In this article, we have understood that the Assert keyword in Java is not a well-known feature of the Java programming language. It helps in removing the boilerplate code from a program and makes the code more readable and convenient for the user. Using assertions in a code helps in identifying bugs at an early stage, which helps in effective program development. Finally, we should always remember that assertions are not enabled by default. They should be enabled or disabled manually by the programmer himself. Else, they will be executed when used in a code.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA