What Is Java Assert?


Quick Answer

Java assert is a keyword statement placed in the Java programming language source code to assist with the testing and debugging process. Developers use assert statements to test assumptions about their programs. When an assert statement is executed, the system behaves in a certain way. Alternatively, the assert statement returns an error message if the system is not configured properly.

Continue Reading
Related Videos

Full Answer

According to Oracle, assert statements have a specific configuration formatted in two different forms. The first format is simply, "assert Expression1 ;" where Expression1 is a Boolean expression. When the system runs the assertion, it evaluates Expression1 and, if it is false, throws an AssertionError with no detail message. The second format is, "assert Expression1 : Expression2 ;" where Expression1 is a Boolean expression and Expression2 is an expression that has a value. It cannot be an invocation of a method that is declared void.

Adding an assert keyword affects existing code, so it's important to do an assessment check for compatibility before using the keyword in any particular program. There are also some circumstances under which an assert keyword should not be used. For example, assertions should not be used for argument checking in public methods or for any work that an application requires for correct operation.

Learn more about Computer Programming

Related Questions