Java 5 和 6 中最有用或最有趣的新语言功能?
我刚才正在浏览代码教程,发现了一些有趣的东西——JDK 5 中引入的 import static
功能:
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 */
}
}
这让我想知道,JDK 5 和 6 中还引入了哪些其他功能我不知道的?还有其他类似的新关键字用法吗?有任何值得注意的新库类或函数吗?
我知道发布说明或更改日志就在那里,我不是在寻找“RTFM”答案。我想知道您认为 JDK 5 或 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Java 5 有很多语法变化:最重要的(我记得的)是:
(当然,如上所述在问题中,静态导入和注释。)
正如 Zwei 的回答所提到的,
java.util.concurrent
也是 Java 5 的一个主要功能,而且 JSR-133 及其内存模型发生了变化(允许 < code>volatile 以合理的方式工作,因此如果您愿意,您可以安全地实现双重检查锁定)。Java 6 没有任何语法变化(我记得);它的许多高影响力的更改都性能相关。就库而言,我最喜欢的“Java 6 新功能”库是脚本支持 (
javax.script
)。Java 5 had a lot of syntactic changes: the most significant (that I remember) are:
(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 allowedvolatile
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
).绝对可以,java.util。并发!!!
在Java6中,ConcurrentSkipListMap作为新的数据结构添加到这个包中,它实际上拯救了我们的项目:)
Absolutely, java.util.concurrent!!!
In Java6, ConcurrentSkipListMap was added as a new data structure to this package, and it practically saved our project :)
我将继续提供一个:Java Web Start applet 支持。
我喜欢使用 JOGL,并且能够仅使用简短的 jnlp 文件真是太好了并且不必编写类加载器、下载本机库代码的代码等来处理通过浏览器获取 JOGL。 (是的,我知道 JOGL 的 appletloader 的事情)
只需 JNLP 文件的一行即可加载 JOGL jar、其依赖项(gluegen、nativewindow、newt)以及当前系统的相应本机库。
jnlp 文件的其余部分仅用于我的应用程序的标题及其 JAR 文件的 URL,仅此而已。
顺便说一句,这是在 JDK 6u10 中引入的。
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.
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.
在 Chris Jester-Young 的列表中添加
但我真正期待的是 函数式编程结构。
To Chris Jester-Young's list add
But what I'm really looking forward to is functional programming constructs.