Java 5 和 6 中最有用或最有趣的新语言功能?

发布于 2024-08-23 22:40:32 字数 543 浏览 6 评论 0原文

我刚才正在浏览代码教程,发现了一些有趣的东西——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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

今天小雨转甜 2024-08-30 22:40:32

Java 5 有很多语法变化:最重要的(我记得的)是:

  • 泛型
  • 枚举
  • For-each 循环
  • 自动装箱和自动拆箱
  • 协变返回
  • 可变参数函数

(当然,如上所述在问题中,静态导入和注释。)

正如 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:

  • 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).

回眸一遍 2024-08-30 22:40:32

绝对可以,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 :)

遇见了你 2024-08-30 22:40:32

我将继续提供一个:Java Web Start applet 支持

我喜欢使用 JOGL,并且能够仅使用简短的 jnlp 文件真是太好了并且不必编写类加载器、下载本机库代码的代码等来处理通过浏览器获取 JOGL。 (是的,我知道 JOGL 的 appletloader 的事情)

只需 JNLP 文件的一行即可加载 JOGL jar、其依赖项(gluegen、nativewindow、newt)以及当前系统的相应本机库。

<extension name="JOGL"
    href="http://download.java.net/media/jogl/builds/archive/jsr-231-2.0-beta10/webstart/jogl-all-awt.jnlp" />

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.

<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.

已下线请稍等 2024-08-30 22:40:32

在 Chris Jester-Young 的列表中添加

  • varargs
  • 注释

但我真正期待的是 函数式编程结构。

To Chris Jester-Young's list add

  • varargs
  • annotations

But what I'm really looking forward to is functional programming constructs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文