Java Reference
In-Depth Information
applications. The rest of the topic helps you put object‐oriented principles into practice with increas-
ing functionality.
annotations
Annotations provide metadata about source code that's not part of the program itself. Unlike com-
ments, which are ignored by the compiler, annotations can provide information for the compiler and
can be processed by some tools to generate code, documentation, or other files.
Annotations always begin with the @ symbol. The compiler knows to expect an annotation fol-
lowing the @ symbol. You may have already seen some examples of annotations, such as @Test ,
@Before , and @After , from the unit testing section in Chapter 6.
Some annotations can replace comments, by providing a more structured format for metadata. This
metadata can then be used to automatically generate documentation for your code. These annota-
tions can be custom‐designed for use by the programmer or for consistency across a department or
company.
Other annotations are predefined in java.lang to be used in specific circumstances, such as
@Deprecated , @Override , and @SuppressWarnings . @Deprecated indicates that the element, such
as a method or class, has been replaced by an improved alternative and should no longer be used.
Whenever an element marked @Deprecated is used in a program, the compiler will give a warning
so the programmer knows to use an alternative. @Override indicates that a subclass is overriding
an element from its superclass. You will learn more about this later in this chapter. By using the
@Override annotation when you intend to override an element, the compiler will warn you if the
method does not properly override a superclass element. @SuppressWarnings instructs the compiler
to suppress specific warnings that it would normally generate. You may have seen this suggested as
a solution for warnings appearing in your projects in Eclipse. Java 8 introduced a new annotation
@FunctionalInterface to indicate that the programmer intends to create a functional interface
according to the Java Language Specification.
Another change in Java 8 is the flexibility to use annotations anywhere a type is used, instead of
only applying to declarations as previous Java versions required. This new flexibility allows better
type-checking analysis, although Java 8 does not provide this type-checking framework itself. One
example of a type annotation is if you were to write a piece of code to ensure that a particular vari-
able is never null. Then you could annotate that variable @NonNull to indicate to the compiler that
this variable will never be assigned to null. The compiler can then check for any discrepancies and
warn you if something remains to be addressed.
overloading methods
As mentioned in the introduction to the chapter, there are several small topics covered here, aside
from the main concepts. This first section covers the concept of overloading methods. Overloading
simply refers to using the same name for more than one method in the same class. Java, and many
other languages, can determine which method you're calling as long as the number or type of
parameters is different in each method. This is illustrated with a few examples.
 
Search WWH ::




Custom Search