gpt4 book ai didi

java - Java 中的断言 : transition timeline

转载 作者:行者123 更新时间:2023-11-30 10:30:50 25 4
gpt4 key购买 nike

我刚刚偶然发现以下内容 ( 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 下而且通过“无标志”调用

都能顺利编译
javac Test.class

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

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

所以这些是我的问题:具体是什么时候发生的?以及为什么 Oracle 一直在每个新版本的文档中添加这个警告(“如果你忘记使用这个标志,程序将无法编译”;看,它是斜体的,甚至!)?让我想知道“什么时候”和“为什么”的不仅仅是无聊的好奇心:四天后我将要为 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 源。

当您编译一个类时,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)。

关于java - Java 中的断言 : transition timeline,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43569831/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com