Java 中的断言:过渡时间表

Assertions in Java: transition timeline

我刚刚偶然发现了以下 ( http://docs.oracle.com/javase/8/docs/technotes/guides/language/assert.html#compatibility ):

Unless you specifically request source mode 1.4 with the -source 1.4 flag, the compiler operates in source mode 1.3. If you forget to use this flag, programs that use the new assert statement will not compile. Having the compiler use the old semantics as its default behavior (that is, allowing assert to be used as an identifier) was done for maximal source compatibility. Source mode 1.3 is likely to be phased out over time.

请注意,这是来自 Java 8 的官方 Oracle 文档 --- 但我以前从未 运行 遇到过这个特定的编译时错误。不仅如此,

class Test{
    public static void main(String[] args){
        int a = 0;
        assert a < 0: "a is not negative!";
    }
}

不仅在 Java 8 下而且在 'flagless' 调用

时都能顺利编译
javac Test.class

with JDK 1.6.0_24(我碰巧拥有的最老的;~2011 年 3 月)。

显然,向 assert 作为新关键字的过渡已经结束。

所以这些是我的问题:具体是什么时候发生的?以及为什么 Oracle 不断在每个新版本的文档中添加这个警告("If you forget to use this flag, programs won't compile";看,它是斜体的,甚至!)? 让我想知道 'when' 和 'why' 的不仅仅是无聊的好奇心:四天后我要为 Java 8 (1Z0-809) 写我的 OCP 考试,我期待所有各种肮脏的东西从黑暗的角落向我扑来...

此官方 Java 指南:http://docs.oracle.com/javase/8/docs/technotes/guides/language/assert.html#compatibility 是为 Java 1.4 版本编写的,该版本引入了以前不可用的断言语句(在 1.3 版本中)。

事实上,本指南来自此页面,该页面引用了每个已发布 Java 版本的一些增强功能: http://docs.oracle.com/javase/8/docs/technotes/guides/language/enhancements.html

它来自这个特定的部分:

Enhancements in J2SE 1.4

Assertion Facility - Assertions are boolean expressions that the programmer believes to be true concerning the state of a computer program. For example, after sorting a list, the programmer might assert that the list is in ascending order. Evaluating assertions at runtime to confirm their validity is one of the most powerful tools for improving code quality, as it quickly uncovers the programmer's misconceptions concerning a program's behavior.

所以,其实并不是文档没有更新的问题。
您必须将此信息视为指定版本的指南:J2SE 1.4.
拥有这种历史有助于概述 Java 语言在各个版本中的演变。


但是您的测试是准确的,您还可以从每个版本更新的 javac 官方文档中获得验证,即 javac 和 JDK 8 不使用默认为 1.3 源。

当您编译 class 时,javac 命令可能会将 source 作为参数,指定要在源代码中接受的主要 JDK 版本。

From the official Java 8 documentation :

-source release

Specifies the version of source code accepted. The following values for release are allowed :

1.3 The compiler does not support assertions, generics, or other language features introduced after Java SE 1.3.

1.4 The compiler accepts code containing assertions, which were introduced in Java SE 1.4.

1.5 The compiler accepts code containing generics and other language features introduced in Java SE 5.

5 Synonym for 1.5.

1.6 No language changes were introduced in Java SE 6. However, encoding errors in source files are now reported as errors instead of warnings as in earlier releases of Java Platform, Standard Edition.

6 Synonym for 1.6.

1.7 The compiler accepts code with features introduced in Java SE 7.

7 Synonym for 1.7.

1.8 This is the default value. The compiler accepts code with features introduced in Java SE 8.

8 Synonym for 1.8.


如果没有指定这个参数,它有一个默认值。
你可以看到 1.8 是默认值。
因此,编译器接受具有 Java SE 8 中引入的功能的源代码(当然包括 1.4 中的 assert)。