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.
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.
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.
When an assertion is executed, Java evaluates the specified 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
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.
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
The assert keyword is commonly used to verify conditions that should always be true during program execution.
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.
| assert | Exception Handling |
|---|---|
| Used to verify program assumptions | Used to handle exceptional conditions |
| Mainly used during development and testing | Used during normal program execution |
| Disabled by default | Exception handling is always active |
| Throws AssertionError when an enabled assertion fails | Throws the specified exception |
| Should not be used for user input validation | Can be used to validate and handle invalid input |
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India