|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
The Java language is one of the most widely used programming languages in the world, so it isn't surprising that changes to it are, at times, controversial. The next several articles in Java 101: The next generation will focus on features added to the Java language from Java 1.4 to Java 8. My goal is to introduce you to a toolbox of essential Java language features, with examples demonstrating both why and how they're used in Java programs. We'll also explore some of the controversy surrounding new features such as generics and lambdas (see the end of this article for my discussion about erasure in generics).
This first article is all about assertions, which were added in Java 1.4, and generics, the first of a handful of important new features introduced in Java 5.
Assertions, introduced in Java 1.4, remain one of the most useful and important additions to the Java language. Assertions are used to codify the requirements that render a program correct or not. Assertions test conditions (aka Boolean expressions) for true values, notifying the developer when such conditions are false. Using assertions can greatly increase your confidence in the correctness of your code.
Do you have questions about Java programming? Get reliable answers from an experienced Java tutor. Jeff Friesen's Java Q&A blog is published weekly to address common programming questions from Java beginners and more experienced developers.
Assertions are compilable entities that execute at runtime, assuming you've enabled them for program testing. You can program assertions to notify you of bugs at the points where the bugs occur, which can greatly reduce the amount of time you would otherwise spend debugging a failing program.
Before Java 1.4, developers mostly used comments to document assumptions about code correctness. While useful for documenting code, comments are inferior to assertions as a testing and debugging mechanism. Because the compiler ignores comments, there is no way to use them for bug notification. It's also common for comments to be unchanged, even when code changes.
Assertions are implemented via the assert statement and java.lang.AssertionError class. This statement begins with the keyword assert and continues with a Boolean expression. An assert statement is expressed syntactically as follows:
assert BooleanExpr;
If BooleanExpr evaluates to true, nothing happens and execution continues. If the expression evaluates to false, however, AssertionError is instantiated and thrown, as demonstrated in Listing 1.
public class AssertDemo
{
public static void main(String[] args)
{
int x = -1;
assert x >= 0;
}
}
The assertion in Listing 1 indicates the developer's belief that variable x contains a value that is greater than or equal to 0. However, this is clearly not the case; the assert statement's execution results in a thrown AssertionError.
Compile Listing 1 (javac AssertDemo.java) and run it with assertions enabled (java -ea AssertDemo). You should observe the following output:
java.util.concurrent.
java.time classes you're most likely to need.