assert

assert keyword is used to create assertions in java, which enables you to test your assumptions about your program. For example, an assertion may be to make sure that an employee’s age is positive number.

Each assertion contains a boolean expression that you believe will be true when the assertion executes. If it is not true, the system will throw an error.

Assertion Syntax

The assertion statement will have either of below given two forms:

assert Expression1;
//or 
assert Expression1 : Expression2 ;

where:

  • Expression1 is a boolean expression.
  • Expression2 is an expression that has a value and this value will be compared with Expression1.

Like all uncaught exceptions, assertion failures are generally labeled in the stack trace with the file and line number from which they were thrown. The second form of the assertion statement should be used in preference to the first only when the program has some additional information that might help diagnose the failure.

How to use assertions

assert can help support in design-by-contract style of programming. It can be used to validate the pre-conditions, post-conditions and other general assertions. e.g

public boolean updateEmployee (Employee e) 
{
	//Employee age must be greater than 18	
	assert e.getAge() > 18 : e.getAge();

	//save employee record
}

Above assertions helps in establishing the condition and employee should be not of minor age.

Enable and Disable Assertions

Sometimes assertions may increase the execution overhead of your program. To ensure that assertions are not a performance liability in deployed applications, assertions can be enabled or disabled when the program is started, and are disabled by default. Once disabled, they are essentially equivalent to empty statements in semantics and performance.

  • To enable assertions at various granularities, use the -enableassertions, or -ea, switch.
  • To disable assertions at various granularities, use the -disableassertions, or -da, switch.

The granularities can be defined like below:

  1. no arguments

    Enables or disables assertions in all classes except system classes.

    java -ea Main

    Above command will run Main.class with assertions enable on all the classes.

  2. packageName…

    Enables or disables assertions in the named package and any subpackages.

    java -ea:com.howtodoinjava.dao... Main

    Above command will run the Main.class, with assertions only enabled in package com.howtodoinjava.dao.

  3. Enables or disables assertions in the unnamed package in the current working directory.

    java -da:... Main

    Above command will run Main.class with assertions disables on all the classes present in current working directory where Main.class is present.

  4. className

    Enables or disables assertions in the named class only.

    java -ea:com.howtodoinjava.dao... -da:com.howtodoinjava.dao.RolesDao Main

    Above command will run the Main.class, with assertions enabled in package com.howtodoinjava.dao; BUT not in class com.howtodoinjava.dao.RolesDao.