⬅ Previous Topic
Java LoggingNext Topic ⮕
Java Keywords⬅ Previous Topic
Java LoggingNext Topic ⮕
Java KeywordsIn Java, assertions act as internal sanity checks for your code. They're used by developers to confirm that the program logic is behaving as expected. If an assertion fails, it typically means there’s a bug in your program — not just a bad input, but a flaw in the code's assumptions.
An assertion is a statement in Java that enables you to test your assumptions about the program during runtime. Assertions should not replace exception handling. Instead, they serve as internal self-checks that help developers catch bugs early in the development phase.
Java provides the assert
keyword in two forms:
// First form
assert condition;
// Second form (with a message)
assert condition : expression;
If the condition
evaluates to false
, an AssertionError
is thrown. The optional expression
provides a custom message for debugging.
public class AssertionDemo {
public static void main(String[] args) {
int age = -5;
assert age >= 0 : "Age cannot be negative";
System.out.println("Age is: " + age);
}
}
Exception in thread "main" java.lang.AssertionError: Age cannot be negative
By default, assertions are disabled at runtime. To enable them, you need to pass the -ea
(or -enableassertions
) flag to the JVM:
java -ea AssertionDemo
Assertions are best used for validating internal states that should never happen under correct program logic. Here are some common scenarios:
There are places where assertions should never be used:
For example, don’t use assertions like this:
// BAD practice
assert email != null : "User must provide an email";
This assumes the user never provides a null email, which is not safe in real-world applications.
public int divide(int a, int b) {
assert b != 0 : "Divider must not be zero";
return a / b;
}
int[] numbers = {2, 4, 6, 8, 10};
for (int num : numbers) {
assert num % 2 == 0 : "All numbers should be even";
}
When an assertion fails, the JVM throws an java.lang.AssertionError
. This error halts the execution of the current thread and provides insight into what assumption was violated.
Technically, yes — but it's discouraged. Assertions are meant for developers, and once they fail, the program shouldn’t attempt to recover. If you find yourself writing try-catch
around assertions, consider whether you should be using exceptions instead.
You can enable assertions for specific classes or packages:
java -ea:com.example... MyApp
Or disable them selectively:
java -da:com.example.MyClass MyApp
Assertions are powerful but sharp tools — meant to be wielded with intention. Use them to fortify your internal logic, not to validate user behavior. When used appropriately, they can catch bugs early and make your codebase more robust and expressive.
assert
to test developer assumptions-ea
and disable with -da
public class TestAssertion {
public static void main(String[] args) {
int x = -1;
assert x >= 0 : "Negative value not allowed";
System.out.println("x is: " + x);
}
}
assert email != null : "User must provide an email";
⬅ Previous Topic
Java LoggingNext Topic ⮕
Java KeywordsYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.