Java 5

Learn about Java 5 and a few of the new features added to it.

Java 5 added several new features to the language. If you’re not familiar with Java 5 or would like a refresher, keep reading.

The following new features have been added to Java 5:

  • Generics
  • Annotations
  • More concise for loops
  • Static imports
  • Autoboxing/unboxing
  • Enumerations
  • Varargs
  • Concurrency utilities in package java.util.concurrent

Generics

Generics were a huge addition to the language. Although it improved the type-safety of Java, it also increased the complexity of the language.

Generics are most commonly used to specify what type a Collection holds. This reduces the need for casting and improves type safety. For example, we can declare a list of strings in the following manner:

List<String> strings = new ArrayList<String>();

Declaring a map of ...