gpt4 book ai didi

java - @interface 是什么时候在 java 中引入的?

转载 作者:行者123 更新时间:2023-11-29 05:14:07 30 4
gpt4 key购买 nike

我知道这个@interface 元素用于在 Java 中定义注释。
我知道注释是在 Java 5 中引入的。

我的问题:

1) 该元素是如何(正式地)命名的,以及从什么时候开始命名的
它是否被编译器识别,即它是何时引入的?
它是注释还是元注释本身?

2) 在 Java 8 之前(例如在 Java 5 或 6 中)是否有一些其他元素/方式
用于定义注解或者它们一直被定义为
自从他们出现在语言中以来的方式?

最佳答案

how is that element called (formally), and since when is it recognized by the compiler i.e. when was it introduced?

annotation type declaration .这是在 Java 5 中添加的。

Is it an annotation or a meta-annotation itself?

不,@interface 本身不是注释,它只是关键字interface 前面有一个@。这不是元注释。元注解是可用于注解类型声明的注解。 JDK 本身有很多这样的(例如 @RetentionPolicy)。

before Java 8 (e.g. in Java 5 or 6) was there some other element/way used for defining annotations

没有。

or they had they always been defined that way ever since their advent in the language?

是的。

Java 5 中添加了注解,@interface 始终是创建注解的方式;过去没有任何其他机制来创建注释。

为什么使用 @interface 而不是 annotation 之类的新关键字:这样做是为了向后兼容。添加新关键字意味着您会立即产生向后兼容性问题,因为为旧版本编写的程序可能已使用关键字名称作为变量名称。这样的程序不会再在新版本上编译。因此,当他们想要向 Java 添加新功能时,Oracle 更愿意重新使用现有关键字而不是创建新关键字。

也就是说,在Java的演进过程中加入了新的关键字,如enumassert(Java 1.4加入)。

关于java - @interface 是什么时候在 java 中引入的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27203236/

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