Sams Teach Yourself Java 2 in 24 Hours
- Table of Contents
- Copyright
- About the Author
- About the Technical Editor
- Acknowledgments
- We Want to Hear from You!
- Reader Services
- Introduction
- Hour 1. Becoming a Programmer
- Hour 2. Writing Your First Program
- Hour 3. Vacationing in Java
- Hour 4. Understanding How Java Programs Work
- Part II: Learning the Basics of Programming
- Hour 5. Storing and Changing Information in a Program
- Hour 6. Using Strings to Communicate
- Hour 7. Using Conditional Tests to Make Decisions
- Hour 8. Repeating an Action with Loops
- Part III: Working with Information in New Ways
- Hour 9. Storing Information with Arrays
- Hour 10. Creating Your First Object
- Hour 11. Describing What Your Object Is Like
- Hour 12. Making the Most of Existing Objects
- Part IV: Programming a Graphical User Interface
- Hour 13. Building a Simple User Interface
- Hour 14. Laying Out a User Interface
- Hour 15. Responding to User Input
- Hour 16. Building a Complex User Interface
- Part V: Creating Multimedia Programs
- Hour 17. Creating Interactive Web Programs
- Hour 18. Handling Errors in a Program
- Hour 19. Creating a Threaded Program
- Hour 20. Reading and Writing Files
- Part VI: Creating Multimedia Programs
- Hour 21. Using Fonts and Color
- Hour 22. Playing Sound Files
- Hour 23. Working with Graphics
- Hour 24. Creating Animation
- Part VII: Appendixes
- Appendix A. Tackling New Features of Java 2 Version 1.4
- Appendix B. Using the Java 2 Software Development Kit
- Appendix C. Programming with the Java 2 Software Development Kit
- Appendix D. Using Sun ONE Studio
- Appendix E. Where to Go from Here: Java Resources
- Appendix F. This Book's Web Site
Assertions
Sun Microsystems did something in Java 2 version 1.4 that hasn't happened often in the history of the language: It added a new keyword, assert.
The assert keyword enables Java programmers to use assertions, a technique that's supposed to improve the reliability of software. An assertion is a Boolean true-or-false expression that represents something that should be true at that spot in the program. Here's an example:
assert speed > 55;
This statement asserts that the speed variable has a value greater than 55. It's another way for a programmer to say, "I expect speed to be greater than 55 at this position, and if it isn't, my program would be completely FUBAR."
Assertions are a way to ensure that a program is running as expected.
The assert keyword must be followed by something that produces a Boolean value: an expression, a boolean, or a method that returns a boolean. Three examples:
assert pointX = 0; assert endOfFileReached; assert network.noLongerConnected();
If the expression used with the assert keyword is false, an AssertionError exception will be thrown. You can make these errors more meaningful by specifying an error message in an assert statement. Add a colon and text at the end, as in this example:
assert temperature > 2200 : "Core breach in Sector 12!";
If you're using the SDK interpreter, here's how it responds to an AssertionError exception:
Exception in thread "main" java.lang.AssertionError: Core breach in Sector 12!
at Nuke.power(Nuke.java:1924)
Assertions are offered in Java 1.4, but are turned off by default in the SDK (and presumably other tools as well).
SDK users must use command-line arguments to turn on assertion support. To compile a class that contains assert statements, use the -source 1.4 option, as in the following example:
javac -source 1.4 Nuke.java
The -source 1.4 option causes the compiler to support assertions in the class file (or files) that it produces. An error will result if you try to compile a program that contains assert statements but do not use this option.
You must also enable assertions when running a Java class with the SDK interpreter. The easiest way to do this is to use the -ea argument, which enables assertions for all classes (except those that are part of the Java class library):
java -ea Nuke
To enable assertions only in one class, follow -ea with a colon and the name of the class, as in this example:
java -ea:Nuke Nuke
To enable assertions only in one package, follow -ea with a colon and the name of the package:
java -ea:com.prefect.power Nuke
Workshop: Throwing and Catching Exceptions | Next Section

Account Sign In
View your cart