Search code examples
javalanguage-featuresjava-6java-5

Most useful or interesting new language features in Java 5 and 6?


I was looking through a code tutorial just now, and found something interesting -- the import static feature introduced in JDK 5:

import static org.junit.Assert.assertEquals;

public class AdditionTest {

  private int x = 1;
  private int y = 1;

  @Test public void addition() {
    int z = x + y;
    assertEquals(2, z);
    /* ^ this is a static method, normally called by Assert.assertEquals */
  }

}

It got me wondering, what other features were introduced in JDK 5 and 6 that I don't know about? Are there other new keyword usages like this? Any noteworthy new library classes or functions?

I know that release notes or changelogs are out there, I'm not looking for an "RTFM" answer. I want to know a short list of, in your opinion, what features you think are most game-changing in JDK 5 or 6.


Solution

  • Java 5 had a lot of syntactic changes: the most significant (that I remember) are:

    • Generics
    • Enums
    • For-each loops
    • Auto-boxing and auto-unboxing
    • Covariant returns
    • Variadic functions

    (And of course, as noted in the question, static imports and annotations.)

    As Zwei's answer mentioned, java.util.concurrent is a major Java 5 feature too, and also, JSR-133 and its memory model changes (that allowed volatile to work in a sane way, so you could implement double-checked locking safely if you wanted to).

    Java 6 didn't feature any syntax changes (that I remember); many of its high-impact changes were performance-related. Library-wise, my favourite "new to Java 6" library was the scripting support (javax.script).