In Java, the assert
keyword is used to test if a given boolean expression is true or false. If the expression is true, the program continues to execute normally. If the expression is false, an AssertionError
is thrown.
To use assert
, you can write an expression followed by a semicolon, like this:
1 2 3 | assert booleanExpression; |
You can also include a message to be displayed when the assertion fails:
1 2 3 | assert booleanExpression : errorMessage; |
For example:
1 2 3 4 | int x = 5; assert x > 0 : "x must be positive"; |
You can also use multiple assertions in a program.
Note that assertions are not enabled by default in Java. To enable them, you can use the -ea
command line option when you run your program.
1 2 3 | java -ea MyProgram |
It is generally recommended to use assertions only for testing and debugging and to not rely on them in production code.
Creating and Using Assertions:
Assertions can be created using the assert keyword. The general form of using assert keyword is as follows:
assert condition;
1 2 3 4 5 6 7 8 9 10 11 | public class Example { public static void main(String[] args) { int x = 5; assert x > 0; // the assertion passes because x is greater than 0 x = -3; assert x > 0; // the assertion fails and an AssertionError is thrown } } |
In this example, the first assertion passes because the value of x
is greater than 0. The second assertion fails, and an AssertionError
is thrown with the default error message “assertion failed”.
You can also include a message to be displayed when the assertion fails:
1 2 3 4 5 6 7 8 9 10 11 | public class Example { public static void main(String[] args) { int x = 5; assert x > 0 : "x must be positive"; // the assertion passes because x is greater than 0 x = -3; assert x > 0 : "x must be positive"; // the assertion fails and an AssertionError is thrown with message "x must be positive" } } |
It is important to note that assertions are not enabled by default in Java. To enable them, you can use the -ea
command line option when you run your program.
1 2 3 | java -ea Example |
It is generally recommended to use assertions only for testing and debugging and to not rely on them in production code.
When the given condition becomes false, AssertionError is thrown by the Java run-time. The second form of assert is as follows:
assert condition : expr;
The assert
statement in Java allows you to test a boolean condition and, if the condition is false, the program will throw an AssertionError
. The assert
statement takes the following form:
1 2 3 | assert condition : expression; |
Where condition
is a boolean expression that you want to test, and expression
is a value or an expression that will be used as the error message.
For example:
1 2 3 4 | int x = 5; assert x > 0 : "x must be positive"; |
In this example, the assertion passes because the value of x
is greater than 0.
If the condition is false, an AssertionError
is thrown with the given error message:
1 2 3 4 | x = -3; assert x > 0 : "x must be positive"; // the assertion fails and an AssertionError is thrown with message "x must be positive" |
The expression that appears after the colon is optional, if it is not provided and the condition is false the AssertionError will be thrown without any message.
1 2 3 4 | x = -3; assert x > 0; // the assertion fails and an AssertionError is thrown without any message |
It is important to note that assertions are not enabled by default in Java. To enable them, you can use the -ea
command line option when you run your program.
1 2 3 | java -ea MyProgram |
It is generally recommended to use assertions only for testing and debugging and to not rely on them in production code.
Enabling and Disabling Assertions:
For enabling them we have to use the following syntax while executing a Java program:
java -ea ClassName
Where -ea denotes enable assertion and similarly for disabling them we can use the following syntax:
java -da ClassName
Where -da denotes disable assertion.
For enabling or disabling assertion in a package we can use the following syntax:
java [-ea | -da] [:package-name… | :ClassName]
Example Program:
Let’s consider a Java program where the numbers entered by the user must not be negative values. To implement this we can use assert keyword as follows:
1 2 3 4 5 6 7 8 9 10 11 12 | import java.util.Scanner; class AssertionDemo { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter n: "); int n = input.nextInt(); assert n > 0; System.out.println("n = " + n); } } |
If the n value is given as -9, then output of the above program is:
1 2 3 4 | Exception in thread “main” java.lang.AssertionError at AssertionDemo.main(AssertionDemo.java:9) |
As the above error message does not give much information we can use second form of assert as shown in the below program:
1 2 3 4 5 6 7 8 9 10 11 12 | import java.util.Scanner; class AssertionDemo { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter n: "); int n = input.nextInt(); assert n > 0: "n cannot be negative"; System.out.println("n = " + n); } } |
Now the output of the above program for -9 as n value is:
1 2 3 4 5 6 | Exception in thread “main” java.lang.AssertionError: n cannot be negative at AssertionDemo.main(AssertionDemo.java:9) |
Take your time to comment on this article.