4

Should Java annotations be considered a language or library feature. I don't mean the very concept of annotations which is obviously a language feature, but specific annotations such as @Override and @Deprecated?

It seems clearer that annotations supported only by 3rd-party libraries (e.g. Hibernate annotations) are clearly not part of the Java language, but for the built-in annotations I'm not so sure.

If this is considered too discursive, feel free to move to programmers.stackexchange.com

Dónal
  • 185,044
  • 174
  • 569
  • 824

3 Answers3

4

Here's how I think about it:

boolean isLanguageFeature(foo)
{
    return JLS.contains(foo)
}

The JLS describes Java the programming language. As you clearly understand, the concept of annotations is a language feature. On the other hand, there are tons of standard Java library features which are not described by the JLS, such as the Collections APIs.

From JLS §9.6.1, Predefined Annotation Types:

Several annotation types are predefined in the libraries of the Java platform. Some of these predefined annotation types have special semantics. These semantics are specified in this section. This section does not provide a complete specification for the predefined annotations contained here in; that is the role of the appropriate API specifications. Only those semantics that require special behavior on the part of the Java compiler or virtual machine are specified here.

Thinking along those lines, then, I'd say that Java annotations are a library feature.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
2

Annotations like @Deprecated and @Override are library features that happen to be used by the language tools to enforce and/or describe code behavior. Definitely library features.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • I agree with you, but you haven't actually provided any supporting argument. – Matt Ball Sep 16 '11 at 00:41
  • 2
    They're in the standard Java library. By definition, they're library features. I don't feel a strong need to support that, I guess. – Dave Newton Sep 16 '11 at 00:46
  • Supporting argument: `@Override` is likely the most language -ish of the standard annotations. Yet, if you wrote an alternative Java compiler that ignores this annotation completely, the resulting code would behave exactly the same as the one `javac` produces. (Assuming it was correct in the first place.) The only way an annotation can change the behaviour of code is if a library (like Hibernate) processes them with a bytecode enhancer. – millimoose Sep 28 '11 at 17:36
  • The fact that `@Override` (library feature) is not required to actually override a function (language feature) makes it even more an (optional) library feature. Omitting `@Override` does not convey different behavior in runtime, only at compile time (extra checks) and in the IDE (warnings). See https://stackoverflow.com/questions/4822954/do-we-really-need-override-and-so-on-when-code-java – xdevs23 Aug 29 '20 at 17:27
  • @xdevs23 ... Yes, that's what we said. – Dave Newton Aug 29 '20 at 18:00
-2

Annotations are used (in Java) for adding meta data to your code. Annotations are the way to write readable and maintainable code. Therefore to write good code use annotations weather they are Java annotations or any other third party.

for e.g. like to make an EJB stateless now you just need to add @Stateless annotation.

  • 2
    I believe his question has more to do with the nature of Java annotations not what they are or when they should be used. – Ian Dallas Sep 16 '11 at 00:13
  • Agreed with @tke. The OP clearly understands how and why to use annotations. This answer misses the question and entirely fails to conclude either way. – Matt Ball Sep 16 '11 at 00:43