Java Reference
In-Depth Information
Listing 10-2. A Program to Check Whether Assertion is Enabled
// AssertionStatusTest.java
package com.jdojo.assertion;
public class AssertionStatusTest {
public static void main(String[] args) {
boolean enabled = false;
assert enabled = true;
if (enabled) {
System.out.println("Assertion is enabled.");
}
else {
System.out.println("Assertion is disabled.");
}
}
}
Summary
Assertions are a feature of the Java language that let you assert in your program for some conditions to hold. The
keyword assert is used to write an assert statement. Assertions are used for detecting logical errors in a program
and they are typically enabled in development and testing environments. Assertions can be enabled and disabled for
packages and classes. They should not be used to validate user's inputs or business rules. Assertions do not replace
exceptions. Rather they complement each other.
 
Search WWH ::




Custom Search