Java assert Keyword

Last Updated : 29 Sept 2026

The assert keyword in Java is used to test assumptions about the state of a program during development and debugging. It checks whether a specified condition is true and throws an AssertionError when the condition is false.

In this chapter, you will learn about the Java assert keyword, its syntax, working, uses, and how to enable assertions while running a Java program.

What is assert Keyword in Java?

The assert keyword is used to verify that a condition is true at a particular point in a Java program. If the condition evaluates to false and assertions are enabled, Java throws an AssertionError.

The assert keyword is mainly used during development and testing to identify conditions that should normally be true.

Syntax of assert Keyword

The assert keyword can be used in two forms:

assert condition;
assert condition : expression;

In the first form, Java checks the specified condition. In the second form, the expression provides additional information when the assertion fails.

How Does assert Work in Java?

When an assertion is executed, Java evaluates the specified condition.

  1. If the condition is true, the program continues normally.
  2. If the condition is false and assertions are enabled, Java throws an AssertionError.
  3. If assertions are disabled, the assertion statement is ignored.

Example of assert Keyword

Example 1: Using assert with a Condition

In the following example, we use the assert keyword to verify that a value is greater than zero.

Java

public class Main {
    public static void main(String[] args) {
        int number = 10;

        assert number > 0;

        System.out.println("Number is valid");
    }
}

Output:

Number is valid

Example 2: Using assert with a Message

In the following example, we use the second form of the assert statement to provide a message when the assertion fails.

Java

public class Main {
    public static void main(String[] args) {
        int age = 15;

        assert age >= 18 : "Age must be 18 or above";

        System.out.println("Eligible");
    }
}

When assertions are enabled, the program throws an AssertionError if the condition is false.

How to Enable Assertions in Java?

Assertions are disabled by default when a Java program runs. They can be enabled using the -ea or -enableassertions option with the java command.

java -ea Main

You can also disable assertions explicitly using the -da or -disableassertions option.

java -da Main

When to Use assert in Java?

The assert keyword is commonly used to verify conditions that should always be true during program execution.

  • Checking assumptions during development
  • Detecting programming errors
  • Verifying internal program conditions
  • Testing class invariants
  • Debugging program logic

assert vs Exception Handling

The assert keyword and exception handling serve different purposes. Assertions are generally used to detect programming errors and assumptions during development, while exceptions are used to handle conditions that may occur during normal program execution.

assertException Handling
Used to verify program assumptionsUsed to handle exceptional conditions
Mainly used during development and testingUsed during normal program execution
Disabled by defaultException handling is always active
Throws AssertionError when an enabled assertion failsThrows the specified exception
Should not be used for user input validationCan be used to validate and handle invalid input

Important Points About assert Keyword

  • assert is a Java keyword.
  • Assertions are disabled by default.
  • Assertions can be enabled using -ea.
  • A failed enabled assertion throws AssertionError.
  • Assertions are mainly intended for development, testing, and debugging.
  • Assertions should not be used as a replacement for exception handling.
  • Assertions should not be used for validating user input or other conditions that must be checked in production.