Java Reference
In-Depth Information
Appendix A. Miscellaneous language updates
In this appendix, we discuss three other language updates in Java 8: repeated annotations, type
annotations, and generalized target-type inference. Appendix B discusses library updates in
Java 8. We don't discuss JDK 8 updates such as Nashorn and Compact Profiles because they're
new JVM features. This topic focuses on library and language updates. We invite you to read
the following links if you're interested in Nashorn and Compact Profiles:
http://openjdk.java.net/projects/nashorn/ and http://openjdk.java.net/jeps/161 .
A.1. Annotations
The annotation mechanism in Java 8 has been enhanced in two ways:
You can repeat annotations.
You can annotate any type uses.
Before we explain these updates, it's worth quickly refreshing what you could do with
annotations before Java 8.
Annotations in Java are a mechanism that lets you decorate program elements with additional
information (note that prior to Java 8 only declarations could be annotated). In other words, it's
a form of syntactic metadata . For example, annotations are popular with the JUnit framework.
In the following code, the method setUp is annotated with the annotation @Before, and the
method testAlgorithm is annotated with @Test:
@Before
public void setUp(){
this.list = new ArrayList<>();
}
@Test
public void testAlgorithm(){
...
assertEquals(5, list.size());
}
Annotations are suitable for several use cases:
 
Search WWH ::




Custom Search