The assert keyword is used in assertion statement which is a feature of the Java programming language since Java 1.4. Assertion enables developers to test assumptions in their programs as a way to defect and fix bugs.

 

1. Syntax of assert statement

 Syntax of an assert statement is as follow (short version):

assert expression1;

 

or (full version):

assertexpression1 : expression2;

 

Where:

  • expression1 must be a booleanexpression.
  • expression2 must return a value (must not return void).
The assert statement is working as follows:

    • If assertion is enabled, then the assert statement will be evaluated. Otherwise, it does not get executed.
    • If expression1 is evaluated to false, an AssertionError error is thrown which causes the program stops immediately. And depending on existence of expression2:
      • If expression2 does not exist, then the AssertionError is thrown with no detail error message.
      • If expression2 does exist, then a String representation of expression2’s return value is used as detail error message.
    • If expression1 is evaluate to true, then the program continues normally.
 

2. Enable assertion in Java



By default, assertion is disabled at runtime. To enable assertion, specify the switch –enableassertions or -ea at command line of java program. For example, to enable assertion for the program called CarManager:

java –enableassertions CarManager

or this for short:

java –ea CarManager

Assertion can be enabled or disable specifically for named classes or packages. For more information on how to enable and disable assertion, read this article.

 

3. Java Assertion examples

 The following simple program illustrates the short version of assertstatement:

public class AssertionExample {
	public static void main(String[] args) {
		// get a number in the first argument
		int number = Integer.parseInt(args[0]);

		assert number <= 10;	// stops if number > 10

		System.out.println("Pass");

	}
}
 When running the program above with this command:

java -ea AssertionExample 15

 

A java.lang.AssertionError error will be thrown:

 

Exception in thread "main" java.lang.AssertionError

 

       at AssertionExample.main(AssertionExample.java:6)

 

But the program will continue and print out “Pass” if we pass a number less than 10, in this command: 

java -ea AssertionExample 8

And the following example is using the full version of assert statement:

public class AssertionExample2 {
	public static void main(String[] args) {

		int argCount = args.length;

		assert argCount == 5 : "The number of arguments must be 5";

		System.out.println("OK");

	}
}
 When running the program above with this command: 

java -ea AssertionExample2 1 2 3 4

it will throw this error:

 

Exception in thread "main" java.lang.AssertionError: The number of arguments must be 5

        at AssertionExample2.main(AssertionExample2.java:6)

 

Generally, assertion is enabled during development time to defect and fix bugs, and is disabled at deployment or production to increase performance.

 

See all keywords in Java.

 

Related Topics:

 

Other Recommended Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Add comment

   


Comments 

#9Carol Clark2020-05-27 16:46
Hello Nam Ha Minh,
This was helpful. Thank you. You really mean the word "detect" instead of "defect", I think.
Carol Clark
Quote
#8Ravi2016-03-02 18:59
Excellent Explanation
Quote
#7mani2015-12-10 04:13
hi nam,
can u give the example of second version of assert i.e
assert exp1: exp2;
Quote
#6Rizwan2015-10-15 13:43
I want to learn complete Java
Quote
#5Nation2015-03-20 01:07
Okay thanks Nam, I got it now.
Quote