ArrayIndexOutOfBoundsException thrown when you access outside the bounds of an array tells you
that your program calculated an index incorrectly, or failed to verify a value to be used as an index. These are
errors that should be corrected in the program code. Given that you can make errors writing any statement it
would be totally impractical to have to declare or catch all the exceptions that could arise from those
errorshence they are unchecked.
Exercise 1.16: Add fields to BadDataSetException to hold the set name and the I/O exception that
signaled the problem so that whoever catches the exception will know details about the error.
1.15. Annotations
Annotations provide information about your program, or the elements of that program (classes, methods,
fields, variables, and so forth), in a structured way that is amenable to automated processing by external tools.
For example, in many organizations all code that gets written must be reviewed by a programmer other than
the author. Keeping track of what code has been reviewed, by whom, and when requires a lot of bookkeeping
and is a task most suited to an automated tooland indeed some code management systems will support this
kind of task. Annotations allow you to provide this review information with the code that is being reviewed.
For example, here is how you could annotate a class with review information:
@Reviewed(reviewer = "Joe Smith", date = 20050331)
public class Point {
// ...
}
An annotation is considered a modifier (like public or static) and should appear before other modifiers,
on a line of its own. It is indicated by an @ character followed by the name of an annotation typein this case
Reviewed. An annotation type is a special kind of interface, whose members are known as elements. Here is
the definition of our Reviewed annotation type:
@interface Reviewed {
String reviewer();
int date();
}
This is very similar to an interface declaration, except the interface keyword is again preceded by the @
character. When you apply an annotation to a program element, you supply values for all of the elements of
the annotation type as shown above: The string "JoeSmith" is used to set the value of the reviewer
element, while the int value 20050331 is used to set the value of the date element. A tool could extract
these values from either the source file, or (more often) a compiled class file, and determine, for example,
whether a class has been reviewed since it was last modified.
While the language defines the syntax of annotation types, their actual purpose is determined by the tools that
recognize them. We won't have anything else to say about annotations until they are discussed in Chapter 15,
except to mention where they can be applied.
1.16. Packages
Name conflicts are a major problem when you're developing reusable code. No matter how carefully you pick