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 开发者_Python百科6.
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
).
Absolutely, java.util.concurrent!!!
In Java6, ConcurrentSkipListMap was added as a new data structure to this package, and it practically saved our project :)
I'll go ahead and put one out there: Java Web Start applet support.
I like to play around with JOGL, and it's so nice to be able to just use a short jnlp file and not have to write classloaders, code to download native library code, etc. to handle getting JOGL through the browser. (yes I know about JOGL's appletloader thing)
Just one line of my JNLP file loads the JOGL jars, its dependencies (gluegen, nativewindow, newt), and the appropriate native libraries for the current system.
<extension name="JOGL"
href="http://download.java.net/media/jogl/builds/archive/jsr-231-2.0-beta10/webstart/jogl-all-awt.jnlp" />
The rest of my jnlp file is just for the title of my application and the URL of its JAR file, and that's about it.
By the way, this was introduced in JDK 6u10.
To Chris Jester-Young's list add
- varargs
- annotations
But what I'm really looking forward to is functional programming constructs.
精彩评论