JDK 5.0 adds several new extensions to the Java language. One of these is called Annotations (a.k.a. MetaData). The idea behind annotations is nothing new – it has been around for quite a while in the form of custom JavaDoc annotations (like XDoclet). The big thing that JDK 5.0 changes is to move those annotations out of the comments and into the code itself.
Usages for annotations include code generation (like XDoclet can do). But since the annotations are actually in the code (and in the class file), their use can reach into class inspection and runtime. For example, EJB and Web Services will use annotations to replace or supplement deployment descriptors. You can use them as markers to enforce design patterns. And there are many more applications possible.
In this session, we'll look at how you can use this new feature to write better code. And we will of course look at lots of code examples.
- Defining custom Annotations
- Annotating your classes
- Reading the annotations – at compile- and run-time
- What standard Annotations are available
- What JSRs are using Annotations
- What other uses are possible
Another new feature in JDK 5.0 is Generics. The most common usage of Generics is with the Collections classes. For example, Generics allows you to create a Set which holds Integers. This is pretty useful. The most obvious effect of using Generics is that you get to quit doing so much type-casting when you retrieve things from the Set. The compiler knows that you have a Set of Integers, and adds the casts for you.
But Generics is much more than a typing aid. The real benefits come because the compiler can now enforce more type-safety in your code. Since it knows you have a Set of Integers, it won't let you add Shapes or Animals to that Set. This moves the error checking from run-time (ClassCastException) to compile-time. This will result in more robust code.
Generics are not limited to the Collections classes, you can also write your own classes to support Generic Types. This is much more difficult than it appears, especially if you are retrofitting an existing class and must maintain runtime compatibility with non-Generic usages of the class.
In this session, we will look at how to use the new Generics feature, how to write Generic classes, and take a peek at what's going on “under the hood”. And of course, we'll have plenty of example programs to look at along the way.
- Generics: what it is... and what it isn't
- Using Generic classes in your code – especially Generic Collections
- Erasure – the way the compiler translates Generics into class files
- Writing classes that support Generics
- “Gotchas” in writing Generic classes
- Upgrading classes to Generics and maintaining compatibility
- Writing Generic methods
Dave Landers is a Staff Engineer with BEA Systems in Boulder, Colorado. His current focus is improving the application deployment and management experience for WebLogic Portal. Dave has been using Java and J2EE in product development and consulting projects for just about as long as they've been around. When he is not at work, Dave spends his time working with the Boy Scouts and enjoys various outdoor activities with his family. If there’s any time left over (and there rarely is these days), he’ll disappear into the basement to do some wood turning.